Iterating over test data in unit tests
Scott David Daniels
scott.daniels at acm.org
Tue Dec 6 00:12:47 EST 2005
Ben Finney wrote:
> Summary: I'm looking for idioms in unit tests for factoring out
> repetitive iteration over test data....
How about something like:
> import unittest, bowling
> class Test_Game(unittest.TestCase):
> def setUp(self):
> """ Set up test fixtures """
> self.game = bowling.Game()
>
def runs(self, throws):
"""Run a series of scores and return the result"""
for throw in throws:
self.game.add_throw(throw)
return self.game.get_score()
> def test_one_throw(self):
> """ Single throw should result in expected score """
self.assertEqual(5, self.runs([5]))
> def test_three_throws(self):
> """ Three throws should result in expected score """
self.assertEqual(5 + 7 + 4, self.runs([5, 7, 4]))
> def test_strike(self):
> """ Strike should add the following two throws """
self.assertEqual(39, self.runs([10, 7, 4, 7]))
There is no reason you cannot write support functions.
--
-Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list