Iterating over test data in unit tests
Scott David Daniels
scott.daniels at acm.org
Tue Dec 6 09:59:05 EST 2005
Ben Finney wrote:
> Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
>> Summary: I'm looking for idioms in unit tests for factoring out
>> repetitive iteration over test data.
>
> Thanks to those who've offered suggestions, especially those who
> suggested I look at generator functions. This leads to::
Here's another way (each test should independently test one feature):
class Test_Game(unittest.TestCase):
""" Test case for the Game class """
score = 0
throws = []
frame = 1
def setUp(self):
""" Set up test fixtures """
self.game = bowling.Game()
def test_score_throws(self):
""" Game score should be calculated from throws """
for throw in self.throws:
self.game.add_throw(throw)
self.assertEqual(self.score, self.game.get_score())
def test_current_frame(self):
""" Current frame should be as expected """
frame = dataset['frame']
for throw in self.throws:
self.game.add_throw(throw)
self.assertEqual(self.frame, self.game.current_frame)
class Test_one(Test_Game):
score = 5
throws = [5]
frame = 1
class Test_two(Test_Game):
score = 9
throws = [5, 4]
frame = 2
class Test_three(Test_Game):
score = 14
throws = [5, 4, 5]
frame = 2
class Test_strike(Test_Game):
score = 26
throws = [10, 4, 5, 7]
frame = 3
--Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list