Unittest testing assert*() calls rather than methods?

Ben Finney ben+python at benfinney.id.au
Wed Sep 28 16:29:18 CEST 2011


Tim Chase <python.list at tim.thechases.com> writes:

>   def test_foo(self):
>     data = (
>       (item1, result1),
>       ... #bunch of tests for fence-post errors
>       )
>     for test, result in data:
>       self.assertEqual(process(test), result)

The sets of data for running the same test we might call “scenarios”.

> When I run my tests, I only get a tick for running one the one test
> (test_foo), not the len(data) tests that were actually performed.

Worse, if one of the scenarios causes the test to fail, the loop will
end and you won't get the results for the remaining scenarios.

> Is there a way for unittesting to report the number of
> passed-assertions rather than the number of test-methods run?

You can use the third-party ‘testscenarios’ library
<URL:http://pypi.python.org/pypi/test-scenarios> to generate test cases
at run time, one for each combination of scenarios with test cases on
the class. They will all be run and reported as distinct test cases.

There is even another library integrating this with Django
<URL:http://pypi.python.org/pypi/django-testscenarios>.

-- 
 \     “Books and opinions, no matter from whom they came, if they are |
  `\     in opposition to human rights, are nothing but dead letters.” |
_o__)                                                  —Ernestine Rose |
Ben Finney



More information about the Python-list mailing list