pytest-dev / pytest-dev/pytest-django

Allow db access to determine length of parameterized fixture list

Open
#385 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I have a periodically-increasing set of regression test-cases in my test database, but I don't see a documented way to look in the django db and use that data to set the length of my parameterized 'regression_test_data' fixture. As a hack, before 3.0, inside 'pytest_generate_tests' I would check if regression tests are being run and, if so, hit the database using the django db manager to determine the number of regression tests, but even this hack fails during collection on 3.0 with 'Failed: Database access not allowed, use the "django_db" mark to enable it.'

Ideally test generation referenced from a django_db test could be enhanced to automatically allow db access. As an added bonus it would be great if any collection of a django_db test could be completely skipped if the command-line is run to ignore tests marked django_db. If this were implemented in version X.Y, then I could do something like:

# test_my_regression.py
@pytest.mark.django_db
def test_my_regression_for_my_function(my_regression_fixture): ...

# conftest.py
def pytest_generate_tests(metafunc):
    if 'my_regression_fixture' in metafunc.fixturenames:
        # This code won't get called during fast tests, because none of
        # my fast tests use my_regression_fixture, and pytest X.Y is smart
        # enough to not even call pytest_generate_tests on those skipped tests.

        # pytest X.Y checks for django_db tag on test_my_regression_for_my_function,
        # and says 'OK, you can use db', rather than raise error
        regressions = Regression.objects.filter(...)
        metafunc.parametrize('my_regression_fixture', regressions)

-Rolf

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 with pytest_generate_tests and the conftest.py example, then trace how django_db-marked tests are handled during collection. The work is done when database access is permitted for generation referenced by django_db tests and collection can skip those tests when django_db tests are excluded.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
database, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.