[py-dev] Running Django unittests using py.test

Morten Brekkevold morten.brekkevold at uninett.no
Mon Nov 1 21:47:30 CET 2010


We're using nose to run our test suite, but we are considering switching to

Some of our tests inherit Django's django.test.TestCase. This class mucks
about with the test invocation machinery inherited from unittest.TestCase to
add simple support for loading of database fixtures (no need to use super()
calls to parent class' setUp/tearDown).

Unfortunately, all these tests will fail when run by py.test, whereas nose
runs thems fine.

After a quick look at the pytest_unittest plugin, it seems it does its own
tinkering and bypasses the normal way that unittest TestCases are invoked.
The setUp, test* and tearDown methods are called explicitly.  AFAIK, to
"manually" run a unittest.TestCase, you instantiate it using the desired test
method's name as an argument.  The resulting instance is callable; calling it
will take care of running setUp, the desired method and the tearDown.

Django's TestCase class hooks into this by overriding
unittest.TestCase.__call__(), and adds pre_setup and post_teardown hooks as
well.  Since py.test never uses TestCase instances as callables, Django
fixtures are never loaded and these tests fail.

I'm not familiar with the inner workings of py.test, so my question is: Is
this behavior intentional? If not, can we change it?

(this can be reproduced using http://dpaste.com/hold/268698/ - which doesn't
require Django, it just rips off Django's TestCase code)

Morten Brekkevold

More information about the Pytest-dev mailing list