Pytest-django: --reuse-db and --create-db not working together

Created on 27 Oct 2016  路  5Comments  路  Source: pytest-dev/pytest-django

I noticed the below issue when using the workflow presented here: https://pytest-django.readthedocs.io/en/latest/database.html#example-work-flow-with-reuse-db-and-create-db. It seems like the behaviour changed in a recent version although it's possible I never noticed this occurred.

When I run the tests passing the --create-db flag, the database is recreated and the migrations are run (notice the time). The database, however, is dropped despite the --reuse-db flag being passed via addopts.

$ pytest tests/app --create-db
...............................................................................................................................
127 passed in 8.23 seconds

When I next run the tests without the --create-db flag the database is again recreated and the migrations are run because it was previously dropped.

$ pytest tests/app
...............................................................................................................................
127 passed in 7.01 seconds

If I run the tests a third time exactly as above, the database is properly reused:

$ pytest tests/app
...............................................................................................................................
127 passed in 1.12 seconds

Most helpful comment

I've had a look at the code and can see a slight logic change in a recent refactor (#362) causes this.

The logic in django_db_setup used to be:

if not request.config.getvalue('reuse_db'):
        request.addfinalizer(teardown_database)

Now it is:

if not django_db_keepdb:
        request.addfinalizer(teardown_database)

where the django_db_keepdb fixture is request.config.getvalue('reuse_db') and not request.config.getvalue('create_db').

It's not a huge deal but it makes the docs here not incorrect but a bit misleading since the database is created twice.

All 5 comments

I've had a look at the code and can see a slight logic change in a recent refactor (#362) causes this.

The logic in django_db_setup used to be:

if not request.config.getvalue('reuse_db'):
        request.addfinalizer(teardown_database)

Now it is:

if not django_db_keepdb:
        request.addfinalizer(teardown_database)

where the django_db_keepdb fixture is request.config.getvalue('reuse_db') and not request.config.getvalue('create_db').

It's not a huge deal but it makes the docs here not incorrect but a bit misleading since the database is created twice.

Thanks for tracking it down! We should fix this and bring back the old behavior for sure.

@pelme Could you please merge pull request from @dekkers ? Seems like it fixes this issue.

Sorry, it is not a pull request. The fix still seems to be valid.

Was this page helpful?
0 / 5 - 0 ratings