Corrected database setup and teardown of bare pytest classes. #16

wants to merge 1 commit into


None yet

4 participants

mjkaye commented Jul 6, 2012

Flushes the database on teardown, and deals with a few other necessities, when a test-case is just a bare pytest class (i.e. not a subclass of Django's TestCase).

pelme commented Jul 6, 2012

Hmm, there are some big changes coming up in the lazy_django branch which will change the way the databases are set up:

Basically all non-django TestCase subclasses must specify the pytest.mark.django_db marker or use a funcarg which requests database access.

I will leave this pull request for now and revisit it after lazy_django has been merged, but I think that this issue will be solved the by adding the appropriate marker to your test class.

mjkaye commented Jul 7, 2012

I think I'll try out the lazy_django branch, then. Hopefully it still supports Django 1.3. Can I ask how stable it is?

It's great to see such significant development in this plugin.

In the meantime, until the lazy_django branch makes it to release, may I suggest that the need for database-accessing test classes to be sub-classes of Django's TestCase (or TransactionTestCase) is documented?

pelme commented Jul 8, 2012

It is not very stable, but the test suite passes for everything except --create-db / --reuse-db. The biggest change is the requirement of the @django_db marker to request database access, which will be backward incompatible. A couple of other things have been deprecated too, like pytest.load_fixutre / pytest.urls.

It would be very helpful if you tried it out and let us know if you have any other suggestions on how to improve it!

I have documented this in b72cd6f to avoid confusion until lazy_django lands.


I believe this bug still persists. I have a test function marked with pytest.mark.django_db and still I can't access the database on setup_function or teardown_function.

Using pytest 2.2.0 (also tested with master)

pelme commented Feb 22, 2013

I will look into it and see if I can figure something out!

@flub Do you have any ideas what might be causing this?

flub commented Feb 22, 2013

This is because fixtures (i.e. setup_fuction and teardown_function) do now know they are being called for a django_db marked test function. If you want to use the database in fixtures you must request database access in the fixture by requesting the "db" fixture (or "transactional_db"). If you check the documentation of "db" it does say this, but maybe the documentation for django_db should make this clear too.

This does make me wonder if we can avoid that limitation for function-scoped fixtures however, I'll have a look at this.

@flub flub was assigned Feb 28, 2013
pelme commented Dec 27, 2013

I'm closing this since all database enabled code must request the database fixtures. pytest-django simply has no way of knowing which setup_function/teardown_function calls that needs to have the database available.

This PR is out of date with the fixture mechanism for setting up the database and cannot be merged anyways.

@pelme pelme closed this Dec 27, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment