For each application in the project, you need to write tests. Also for each application you first need to upload your test data, which, after passing all the module tests, must be deleted.
I found several solutions, but none of them seems to me optimal
First:
in file conftest.py in each app I override method django_db_setup, but in this case, the data is not deleted after passing the tests in the module, and become available for other applications.
In theory, with the help of yield you can delete all the data after passing the tests.
@pytest.fixture(scope='module')
def django_db_setup(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
call_command('loaddata', './apps/accounts/fixtures/accounts.json')
call_command('loaddata', './apps/activation/fixtures/activation.json')
call_command('loaddata', './apps/questionnaire/fixtures/questionnaire.json')
yield
# delete test data
Second: in the class with tests write such a setup
@pytest.fixture(autouse=True)
def setup(self, db):
call_command('loaddata', './apps/accounts/fixtures/accounts.json')
call_command('loaddata', './apps/activation/fixtures/activation.json')
call_command('loaddata', './apps/questionnaire/fixtures/questionnaire.json')
In this case, the data will be loaded exactly as many times as there will be tests in the module, which also seems to be not quite correct.
I did something like this in my own tests :
from pytest_django.fixtures import _django_db_fixture_helper
@pytest.fixture(autoscope='module')
def setup_db(request, django_db_setup, django_db_blocker):
_django_db_fixture_helper(request,·django_db_blocker)
call_command('loaddata', 'path/to/fixture.json')
I think that pytest_django should export the _django_db_fixture_helper in its official API as a factory fixture.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With