0% found this document useful (0 votes)
18 views1 page

Battleship Test Block

The document contains a Python script that uses the unittest library to define test cases for a game application. It includes test classes for the Game and Player, verifying that a game creates exactly two players and that each player has two grids. The script also includes a function to run all defined tests automatically.

Uploaded by

ii mu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Battleship Test Block

The document contains a Python script that uses the unittest library to define test cases for a game application. It includes test classes for the Game and Player, verifying that a game creates exactly two players and that each player has two grids. The script also includes a function to run all defined tests automatically.

Uploaded by

ii mu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# you must import the unittest library

import unittest

# now create a test class for each game class, such as


# TestGame
# TestPlayer
# Test Grid

class TestGame(unittest.TestCase):
# in the test class, each individual test must start with "test_"
def test_game_creates_exactly_two_players(self):
game = Game("A", "B")
self.assertEqual(len(game.players), 2)

class TestPlayer(unittest.TestCase):
def test_each_player_has_two_grids(self):
game = Game("A", "B")
gridcount = 0
for player in game.players:
gridcount = gridcount + len(player.grids)
self.assertEqual(gridcount, len(game.players)*2)

# -----------------------------------------------------------------------------
# now use the following bit of code to run all of your tests
# unittest will automatically find and run them, if you've named them according
# to the convention above
def run_tests():
# be sure to add all of your test classes to this array!
test_classes = [TestGame, TestPlayer]

suite = unittest.TestSuite()
for test_class in test_classes:
tests = unittest.TestLoader().loadTestsFromTestCase(test_class)
suite.addTests(tests)
unittest.TextTestRunner().run(suite)

run_tests()
# -----------------------------------------------------------------------------

You might also like