[py-dev] improving unittest support (was: Re: Running Django unittests using py.test)

holger krekel holger at merlinux.eu
Mon Nov 1 23:19:30 CET 2010

Hi Morten,

On Mon, Nov 01, 2010 at 21:47 +0100, Morten Brekkevold wrote:
> We're using nose to run our test suite, but we are considering switching to
> py.test.
> 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.

Indeed, py.test (so far) only covers relatively plain 
unittest.TestCase usages.

> 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.

Originally the contributor (Guido Wesdorp) and me did this to maintain
py.test's own separation of setup/teardown/call phases IIRC.  

> 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?

It is intentional but we can (try to) 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)

Thanks for providing the paste and your precise helpful info. 
As it happens i am in the process of preparing a pytest-2.0 and just
went ahead and changed the unittest-plugin to invoke the test case
as you described.  This lets your example run.   You can install it via::

    pip install -i http://pypi.testrun.org pytest

best in a virtualenv.  Typing "py.test --version" should 
give you at least 2.0.0.dev18

Does this work better for you?

sidenote: the unittest-plugin looks like this now::



> -- 
> Morten Brekkevold
> _______________________________________________
> py-dev mailing list
> py-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/py-dev


More information about the Pytest-dev mailing list