Issue
I have several applications in my project each application has its own tests: app/tests/test_block1.py test_block2.py ... And every tests folder has its own conftest.py I want to drop database for every app. I use this fixture:
@pytest.fixture(scope="package")
def django_db(django_db_blocker):
with django_db_blocker.unblock():
call_command("sqlflush")
with django_db_blocker.unblock():
call_command("migrate", "--noinput")
yield None
Scope function and class does not fits for me. I tried module scope and all tests outside a single file fails. I tried package scope and all tests outside a single folder fails. I tried session scope and behaviour was the same. What will be the good variant in my case? Is there any variant except writting one big conftest file instead of different conftest files for every application?
Solution
You should be in good shape with module
scope. You should define your db fixture in one conftest.py
, in a location that is root location that is common to all the different applications that need it. With this arrangement, the fixture should be available for any test and any module where it is accessed at least once will share the same instance of the DB across all tests in that test module (ie, individual .py file).
It looks to me like you should also be referencing the django_db_setup fixture so that it can be sure to do its one-time setup steps, if any.
The way you are using the django fixture, in your own fixture, looks wrong to me though. I would expect something like this (thought I don't know exactly what you are trying to do):
# Again, defining this in just one conftest.py, in a central location
@pytest.fixture(scope="module")
def django_db(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
call_command("sqlflush")
call_command("migrate", "--noinput")
# Putting the yield here means that you keep the unblocked context
# while your ptest test runs, which is surely what you want
yield None
Answered By - Bill Huneke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.