Unittest testing assert*() calls rather than methods?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Sep 28 20:16:30 EDT 2011

Tim Chase wrote:

> While I asked this on the Django list as it happened to be with
> some Django testing code, this might be a more generic Python
> question so I'll ask here too.
> When performing unittest tests, I have a number of methods of the
> form
>    def test_foo(self):
>      data = (
>        (item1, result1),
>        ... #bunch of tests for fence-post errors
>        )
>      for test, result in data:
>        self.assertEqual(process(test), result)
> 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.  Is there a way for unittesting to report the number
> of passed-assertions rather than the number of test-methods run?

I used to ask the same question, but then I decided that if I wanted each
data point to get its own tick, I should bite the bullet and write an
individual test for each.

If you really care, you could subclass unittest.TestCase, and then cause
each assert* method to count how often it gets called. But really, how much
detailed info about *passed* tests do you need? 

If you are writing loops inside tests, you might find this anecdote useful:



More information about the Python-list mailing list