The thing is that I need to have a conftest with a special setting for a set of tests. To use a live database in readonly mode only for these tests. Other tests from unit folder working with an empty db created by pytest. If I run pytest tests
or pytest tests/unit
conftest from tests_readonly_db are not recognized. It is recognized when I put conftest file in unit folder. And run any of these two commands.
But it works perfectly when I run pytest tests/unit/tests_readonly_db
with conftest in tests_readonly_db folder. Also tried to creat another subfolder in tests_readonly_db, put tests in there, with conftest.py staying level up. Doesn't work.
I wonder if there is way to implement desired behaviour. Found some related answers, but couldn't fully understand how it helps in my case. For example: https://stackoverflow.com/a/13686206/7744657
But if
conftest.py files from sub directories are by default not loaded at tool startup.
Why it was loaded from a unit folder?
conftest.py
import pytest
@pytest.fixture(scope='session')
def django_db_setup():
"""Avoid creating/setting up the test database"""
pass
@pytest.fixture
def db_access_without_rollback_and_truncate(request, django_db_setup, django_db_blocker):
django_db_blocker.unblock()
request.addfinalizer(django_db_blocker.restore)
App structure:
EDIT
I've just tested this with a simple and stupid fixture. And it works. So it's actually a problem with this specific database settings not being discovered in some cases. This simple fixture seems to work in any case.
import pytest
@pytest.fixture(scope='session')
def django_db_setup():
"""Avoid creating/setting up the test database"""
pass
@pytest.fixture
def db_access_without_rollback_and_truncate(request, django_db_setup, django_db_blocker):
django_db_blocker.unblock()
request.addfinalizer(django_db_blocker.restore)
@pytest.fixture
def msg():
return 'AAAAAAAAAAA----------------------------------------AAAAAAAAAA'
tests_views.py
@pytest.mark.django_db()
@pytest.mark.parametrize('uri, args', [
some parameters.....
])
def test_views(uri, args, client, msg):
print(msg)
username = "john doe"
password = "123456"
client.login(username=username, password=password)
if args:
response = client.get(reverse(uri, args=args))
else:
response = client.get(reverse(uri))
assert response.status_code == 200