0

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:

enter image description here

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
edvard_munch
  • 266
  • 1
  • 5
  • 17
  • Add conftest.py in folder above the unit and check if it works. – Amit K. Jun 11 '20 at 15:59
  • @AmitK. if conftest.py in tests/ It behaves the same way as if I put conftest.py in tests/unit/. So it works – edvard_munch Jun 11 '20 at 16:04
  • Yes, Then I guess empty contest.py at /tests and your specif changes in /tests/unit is what you have to do. – Amit K. Jun 12 '20 at 03:36
  • Add two contests.py one at /tests which will be empty and another at /tests/unit with your required changes. This will do the trick. – Amit K. Jun 12 '20 at 03:51
  • @AmitK. I've tried this and it did not work. And then I've realized that I didn't test this with a simpler fixture. So I've actually put this fixture in the same conftest `@pytest.fixture` `def msg():` `return 'AAAAAAAAAAA----------------------------------------AAAAAAAAAA'` Then invoke this fixture in tests from tests_readonly_db and it works in any case with just this one conftest.py. For this fixture. It is just for some reason skips this database settings. If I did not `pytest` this folder directly. Not sure why. Maybe something wrong with the `scope`. – edvard_munch Jun 12 '20 at 09:53
  • Also, I was under impression that I don't need to explicitly state in a test that I'd like to use this fixtures from conftest.py https://pytest-django.readthedocs.io/en/latest/database.html#use-a-read-only-database. It is being used automatically if it's in conftest.py and I have the `@pytest.mark.django_db()` decorator before the test function. Maybe I need in some cases. – edvard_munch Jun 12 '20 at 09:58

0 Answers0