Skip to content

fix: regression in fixture order with pytest-django#55

Merged
NyanKiyoshi merged 2 commits intomainfrom
fix/regression/wrong-fixture-order
Mar 1, 2026
Merged

fix: regression in fixture order with pytest-django#55
NyanKiyoshi merged 2 commits intomainfrom
fix/regression/wrong-fixture-order

Conversation

@NyanKiyoshi
Copy link
Copy Markdown
Owner

@NyanKiyoshi NyanKiyoshi commented Mar 1, 2026

Since v1.3.0rc1, the db fixture from pytest-django may be only initialized after the fixture count_queries is loaded thus leading to such errors:

______________________________________________ ERROR at setup of test_index_page ______________________________________________

request = <SubRequest '_pytest_query_marker' for <Function test_index_page>>

    @pytest.fixture(autouse=True)
    def _pytest_query_marker(request):
        """Use the fixture to count the queries on the current node if it's
        marked with 'count_queries'.

        Optional keyword-arguments:
            - autouse (bool, default: True)
              Whether the fixture should be used automatically.
              This might be useful if you are executing fixtures
              that are making queries and still want to mark the test
              but place the fixture manually."""
        marker = request.node.get_closest_marker(PYTEST_QUERY_COUNT_MARKER)
        if marker:
>           _process_query_count_marker(request, *marker.args, **marker.kwargs)

../src/pytest_django_queries/plugin.py:142:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../src/pytest_django_queries/plugin.py:126: in _process_query_count_marker
    request.getfixturevalue(PYTEST_QUERY_COUNT_FIXTURE_NAME)
../src/pytest_django_queries/plugin.py:207: in count_queries
    with CaptureQueriesContext(connection) as context:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.test.utils.CaptureQueriesContext object at 0x7f65bc991b20>

    def __enter__(self):
        self.force_debug_cursor = self.connection.force_debug_cursor
        self.connection.force_debug_cursor = True
        # Run any initialization queries if needed so that they won't be
        # included as part of the count.
>       self.connection.ensure_connection()
E       RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

This fixes the issue by no longer assuming db fixture is loaded first. This assumption was likely accidental and adding official support for pytest ≥ 7.2.0 may only have surfaced the bug.

Additionally, this commit fixes example not being in the PATH by default.

Changes Checklist

  • The changes were tested (manually)
  • The changes are tested automatically (pytest)
  • The changes are optimized and clean
  • The changes are documented:
    • The code is documented
    • The readme is up to date
    • The documentation (readthedocs) is up to date and tested

Since v1.3.0rc1, the `db` fixture from `pytest-django` may be only initialized
after the fixture `count_queries` is loaded thus leading to such errors:

```
______________________________________________ ERROR at setup of test_index_page ______________________________________________

request = <SubRequest '_pytest_query_marker' for <Function test_index_page>>

    @pytest.fixture(autouse=True)
    def _pytest_query_marker(request):
        """Use the fixture to count the queries on the current node if it's
        marked with 'count_queries'.

        Optional keyword-arguments:
            - autouse (bool, default: True)
              Whether the fixture should be used automatically.
              This might be useful if you are executing fixtures
              that are making queries and still want to mark the test
              but place the fixture manually."""
        marker = request.node.get_closest_marker(PYTEST_QUERY_COUNT_MARKER)
        if marker:
>           _process_query_count_marker(request, *marker.args, **marker.kwargs)

../src/pytest_django_queries/plugin.py:142:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../src/pytest_django_queries/plugin.py:126: in _process_query_count_marker
    request.getfixturevalue(PYTEST_QUERY_COUNT_FIXTURE_NAME)
../src/pytest_django_queries/plugin.py:207: in count_queries
    with CaptureQueriesContext(connection) as context:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.test.utils.CaptureQueriesContext object at 0x7f65bc991b20>

    def __enter__(self):
        self.force_debug_cursor = self.connection.force_debug_cursor
        self.connection.force_debug_cursor = True
        # Run any initialization queries if needed so that they won't be
        # included as part of the count.
>       self.connection.ensure_connection()
E       RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
```

This fixes the issue by no longer assuming `db` fixture is loaded first.
This assumption was likely accidental and adding official support for pytest
≥ 7.2.0 may only have surfaced the bug.

Additionally, this commit fixes `example` not being in the PATH by default.
@NyanKiyoshi NyanKiyoshi self-assigned this Mar 1, 2026
@NyanKiyoshi NyanKiyoshi added the bug Something isn't working label Mar 1, 2026
This makes the workaround from 06db7b8 work against installations that do not use `pytest-django` as our plugin does not require users to install it.
@NyanKiyoshi NyanKiyoshi force-pushed the fix/regression/wrong-fixture-order branch from 1da574f to 3650436 Compare March 1, 2026 15:29
@NyanKiyoshi NyanKiyoshi marked this pull request as ready for review March 1, 2026 15:31
@NyanKiyoshi NyanKiyoshi merged commit 91222c6 into main Mar 1, 2026
17 checks passed
@NyanKiyoshi NyanKiyoshi deleted the fix/regression/wrong-fixture-order branch March 1, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant