Iterating over test data in unit tests

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Dec 6 00:34:40 EST 2005


Scott David Daniels <scott.daniels at acm.org> wrote:
> Ben Finney wrote:
> > Summary: I'm looking for idioms in unit tests for factoring out
> > repetitive iteration over test data....
> 
> How about something like:
> 
> >     class Test_Game(unittest.TestCase):
[...]
>            def runs(self, throws):
>                """Run a series of scores and return the result"""
[...]
> >         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]))

Yes, I'm quite happy that I can factor out iteration *within* a single
data set. That leaves a whole lot of test cases identical except for
the data they use.

The question remains: how can I factor out iteration of *separate test
cases*, where the test cases are differentiated only by the data they
use? I know at least one way: I wrote about it in my (long) original
post. How else can I do it, with less ugliness?

-- 
 \      "I went to a garage sale. 'How much for the garage?' 'It's not |
  `\                                     for sale.'"  -- Steven Wright |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list