pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.4k stars 345 forks source link

No transaction support despite `transactional_db` (edge case of indirect parametrization) #1157

Open scur-iolus opened 1 month ago

scur-iolus commented 1 month ago

I've got tests which rely on a database with initial data (which is possible as explained here). I want to check that the initial data are present as expected and that my custom setup/teardown functions work well. Whether I use db (TestCase) or transactional_db (TransactionTestCase) should not matter in my test suite.

Thus, I wanted to use indirect parametrization as suggested here, in order not to write twice the same tests (DRY)...

@pytest.mark.parametrize("db_access", ["db", "transactional_db"], indirect=True)
class TestInitialData:
    @pytest.fixture(autouse=True)
    def db_access(self, request):
        return request.getfixturevalue(request.param)

    def test_example(self):
        pass

    # many other tests

No error is raised in this example, but it turns out (though that was hard to notice) that it doesn't work as expected: all tests use django.test.TestCase and are run twice without transaction support.

Admittedly, that's an edge case, but after having carefully read the documentation of pytest, pytest-django and django itself, I think it can be considered a bug.

I would say that the issue stems from the implementation of pytest_django.fixtures._django_db_helper which don't take into account indirect parametrization. Am I missing something?