I contribute to pytest-django, which provides Django integration to the pytest test framework. One of the features missing in pytest-django is an equivalent of
setUpTestData() - a way to share DB setup between tests, mostly as a time-saving optimization.
setUpTestData works only at the scope of a single test class. pytest has a more elaborate fixture system and the expectation is that the feature will work with any scope (class, module, package, entire test session). For those familiar with pytest, my current idea is this:
@pytest.fixture(scope='module') def my_item(django_test_data): return Item.objects.create()
I have this implemented as a proof of concept. The
django_test_data bit causes the fixture to wrap its corresponding scope with
In addition to “native” pytest-django tests, pytest-django also supports Django’s unittest tests, which can be intermingled freely. Django’s
TestCase closes all database connections during teardown. Now suppose the
my_item fixture is active in some test module, and the module also contains a Django
TestCase. The connection closing(s) terminates the fixture’s transaction(s) prematurely when the class is torn down, causing all subsequent tests in the module to fail.
Why does Django’s
TestCase close the connections? Naively, it just seems to add overhead, since connections are still usable after rollback. As a test, I ran Django’s test suite using sqlite with these lines removed, and all of the tests pass.
Note: this was originally an email, but apparently you need a google account to post there - eek.