# 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()
# -----------------------------------------------------------------------------