Opened 15 years ago
Last modified 4 weeks ago
#11505 new New feature
Django's TestCase should reset the cache
Reported by: | andrewfong | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Normal | Keywords: | cache testing flush |
Cc: | Mikhail Korobov, jim.dalton@…, hv@…, someuniquename@…, hirokiky@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In between test cases, the cache should be flushed in order to prevent one test case from polluting another, similar to how the database is currently flushed (or transactions rolled back) in Django's test case.
This will be easier to implement if #11503 is complete
Attachments (4)
Change History (28)
comment:2 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
by , 15 years ago
Attachment: | patch.diff added |
---|
comment:3 by , 15 years ago
Has patch: | set |
---|---|
Needs documentation: | set |
comment:4 by , 15 years ago
Needs documentation: | unset |
---|
comment:5 by , 14 years ago
Cc: | added |
---|
There is a pattern to run tests on production machine. With this patch e.g. production memcached instance can be flushed after every commit. Databases are different because one DB server can handle several databases and test database can be created during test setup. I think it can't be done for 'test memcached instance'.
Memcached key space can be easily shared between production and test environment using custom cache backend that prepends all key with fixed string (giving us an equivalent of 'test database' inside single memcached instance). But invalidation is nearly impossible.
comment:6 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
follow-up: 10 comment:7 by , 13 years ago
Easy pickings: | unset |
---|---|
UI/UX: | unset |
#16401 is a duplicate and contains a much more thorough patch.
comment:8 by , 13 years ago
Type: | Bug → New feature |
---|
comment:9 by , 13 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Patch needs improvement: | set |
comment:10 by , 13 years ago
Cc: | added |
---|---|
Needs tests: | unset |
Replying to julien:
#16401 is a duplicate and contains a much more thorough patch.
Thanks julien for consolidating the new ticket I opened back to this one.
I'm uploading the latest patch from that ticket here. In a nutshell, this approach monkey patches cache and get_cache during test setup and keeps track of any keys used during the test. After each test, those keys are flushed. We also prepend '_test' to the key prefix to ensure existing values are not overwritten. The patch is tested and working. I'm leaving it as "needs improvement" for now mainly because there is one test in the suite that is failing and I haven't determined why yet. It's minor whatever is is though.
In sum, the patch works great and solves the problem. There was a bit of discussion here about the approach, however: https://groups.google.com/forum/#!topic/django-developers/zlaPsP13dUY . Jacob stated he wasn't necessarily a fan of monkey patching and suggested an alternative. I had some thoughts which I posted in response as well.
If anyone reading this comment is interested in help push this ticket toward resolution, a great way would be to review this patch and then weigh in on that django dev discussion. If we can reach a consensus on django-dev I'd be happy to put in some time to implement whatever approach is agreed upon.
by , 13 years ago
Attachment: | restore_cache_during_tests.diff added |
---|
comment:11 by , 13 years ago
Patch needs improvement: | unset |
---|
Adding a new patch for this, which extends the modified cache approach to cache middleware. I had noticed on some of my projects that certain tests were failing when using cache middleware, which turned out to be because the cache was returning the response and thus the context was not included in the response to the test client. Tests reproducing this error state as well as a fix are included in the latest patch. The full test suite passes as well (minus 5 unrelated failures that are on trunk right now).
The failures to me are a good illustration of why this patch is important. The system should ideally be as clean as possible in between each test; cache values carrying over from test to test create problematic behavior when a full test suite is run.
I might post again to the dev list if there's no movement on this. If anyone on the CC list here would like to review this patch and give any feedback please do so. At this point, all that's really required from what I can see is a few notes in the documentation.
by , 13 years ago
Attachment: | restore_cache_during_tests.v2.diff added |
---|
comment:13 by , 13 years ago
Cc: | added |
---|
comment:14 by , 12 years ago
Cc: | added |
---|
comment:15 by , 11 years ago
Cc: | added |
---|
comment:16 by , 10 years ago
I ran into another example of tests depending on each other execution, because of the way django.contrib.sites.models.Site
instances are cached in a global variable SITE_CACHE
Reproductible with the command python tests/runtests.py contenttypes_tests.tests.ContentTypesViewsTests.test_shortcut_with_absolute_url syndication_tests
comment:25 by , 4 weeks ago
You can do this automatically for all tests by adding a pytest fixture like this. The operative part is autouse=True
from django.conf import settings from django.core.cache import cache @pytest.fixture(autouse=True) def reset_cache(): yield assert ( settings.CACHES['default']['BACKEND'] == 'django.core.cache.backends.locmem.LocMemCache' ) cache.clear()
Adds cache flushing after every testcase + a simple regression test