Sitelet https://github.com/pytest-dev/pytest-django/pull/1312
Skip to content

Clear ContentType cache between tests - #1312

Open
avenet wants to merge 2 commits into
pytest-dev:mainfrom
avenet:issue-1156-clear-contenttypes-cache
Open

avenet wants to merge 2 commits into
pytest-dev:mainfrom
avenet:issue-1156-clear-contenttypes-cache

Conversation

@avenet

@avenet avenet commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1156.

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"
  • .env/bin/ruff check pytest_django/plugin.py tests/test_environment.py

@kingbuzzman

Copy link
Copy Markdown
Member

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:

  1. 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.)
  2. 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?

@bluetech thoughts?

@avenet

avenet commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

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:

  1. 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.)
  2. 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?

@bluetech thoughts?

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ContentTypes cache not cleared between tests

2 participants