You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clear Django's ContentType cache before each test when django.contrib.contenttypes is installed. This prevents cache state from affecting query-count assertions based on test order.
Tests:
.env/bin/python -m pytest tests/test_environment.py -k "clear_site_cache or clear_contenttypes_cache"
Thanks for this! I agree with the reasoning, but I think it does too much, and does it quietly. Pretty much every Django project I work on has some version of this in a conftest, so I understand the motivation. A couple of thoughts:
If this were something every test needed, I'd expect Django to handle it in its own TestCase. Since it doesn't, I'm hesitant for pytest-django to diverge from Django's behavior by default. (I know we already do something similar for the Site cache, but I'd rather not widen that pattern without a good reason.)
Turning this on for everyone changes behavior for existing suites. Could we make this opt-in instead, either as a fixture users can request or enable with autouse, or as an ini option?
Thanks for this! I agree with the reasoning, but I think it does too much, and does it quietly. Pretty much every Django project I work on has some version of this in a conftest, so I understand the motivation. A couple of thoughts:
If this were something every test needed, I'd expect Django to handle it in its own TestCase. Since it doesn't, I'm hesitant for pytest-django to diverge from Django's behavior by default. (I know we already do something similar for the Site cache, but I'd rather not widen that pattern without a good reason.)
Turning this on for everyone changes behavior for existing suites. Could we make this opt-in instead, either as a fixture users can request or enable with autouse, or as an ini option?
Thanks @kingbuzzman, I agree this shouldn't be on by default.
Happy to wait for @bluetech, or close this in favor of a short docs note if a fixture feels like too much API for a one-liner. I’ve been experimenting locally with an opt-in django_clear_contenttypes_cache fixture in case that’s the direction you’d prefer.
This branch has not been deployed
No deployments
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
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.
Closes #1156.
Clear Django's ContentType cache before each test when
django.contrib.contenttypesis installed. This prevents cache state from affecting query-count assertions based on test order.Tests:
.env/bin/python -m pytest tests/test_environment.py -k "clear_site_cache or clear_contenttypes_cache".env/bin/ruff check pytest_django/plugin.py tests/test_environment.py