pytest-dev / pytest-dev/pytest-django

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

Open
#1,157 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pytest_django/fixtures.py at _django_db_helper and reproduce the indirect-parametrization example from the issue. Check how db and transactional_db are selected, then verify that the transactional_db parameter causes tests to run with transaction support rather than both runs using Django TestCase.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
databases, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.