fix: regression in fixture order with pytest-django#55
Merged
NyanKiyoshi merged 2 commits intomainfrom Mar 1, 2026
Merged
Conversation
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.
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.
1da574f to
3650436
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since v1.3.0rc1, the
dbfixture frompytest-djangomay be only initialized after the fixturecount_queriesis loaded thus leading to such errors:This fixes the issue by no longer assuming
dbfixture 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
examplenot being in the PATH by default.Changes Checklist