I am defining a pytest fixture that to overrides the django_db_setup fixture.
The change I have sets up additional teardown for safety, as there are integration tests th
To inject custom behavior before the initial fixture is called you can create separate fixture with this behavior and use it before the initial fixture in parameter list of fixture that overrides previously defined:
@pytest.fixture(scope='session')
def inject_before():
print('inject_before')
@pytest.fixture(scope='session')
def django_db_setup(inject_before, django_db_setup):
print('inject_after')
There is a simple trick to redefine a fixture with a custom impl. Just declare a fixture with the same name and signature in your local test code (I usually do it in the conftest.py
in project root). Examples:
# conftest.py
import pytest
@pytest.fixture(scope='session')
def django_db_setup(
request,
django_db_setup,
django_test_environment,
django_db_blocker,
django_db_use_migrations,
django_db_keepdb,
django_db_createdb,
django_db_modify_db_settings,
):
# do custom stuff here
print('my custom django_db_setup executing')
Notice I have django_db_setup
argument in the custom django_db_setup
fixture - this ensures the original fixture is called before the custom one.
If you omit the argument, the custom fixture will replace the original one, so it won't be executed:
@pytest.fixture(scope='session')
def django_db_setup(
request,
django_test_environment,
django_db_blocker,
django_db_use_migrations,
django_db_keepdb,
django_db_createdb,
django_db_modify_db_settings,
):
print((
'my custom django_db_setup executing - '
'original django_db_setup will not run at all'
))
BTW, this is another handy trick to use when you e.g. want to turn off a fixture that is defined elsewhere.