pytest-django
pytest-django copied to clipboard
ContentTypes cache not cleared between tests
We've been testing our query counts, and found our tests weren't independent due to the caching of ContentTypes causing query counts to depend on test ordering.
There's a handy pytest-django fixture for clearing the Sites cache but not one for ContentTypes.
Is this something that would be accepted as an addition to this library? It would call ContentType.objects.clear_cache()
The downside is that it will slow down tests when this cache is beneficial.
Is it a complete hassle to clear the cache manually before sensitive num-queries assertions? If yes, if we add a fixture but not autouse would that still be too much hassle?
I just spent hours tracking down why queries were inconsistent in my test suite, and have found myself here.
It's important to me to have the same number of queries in every test.
I also understand why it might be undesirable to clear this cache in tests when it's not required.
Perhaps another alternative would be to have a way to ensure the cache is populated before tests begin?
EDIT: FWIW, I've found that clearing the cache on every test run works well for me.