override_settings(ROOT_URLCONF) doesn't clear resolver cache
|Reported by:||gcc||Owned by:||gcc|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
If override_settings is used to set different URL mappings on different tests, by setting ROOT_URLCONF to different values, it doesn't work properly because the resolver cache is never cleared.
You might argue that this behaviour is unnecessary or broken, but Django-CMS for example recommends that you do:
Your apps need testing, but in your live site they aren’t in urls.py as they are attached to a CMS page. So if you want to be able to use reverse() in your tests, or test templates that use the url template tag, you need to hook up your app to a special test version of urls.py and tell your tests to use that... in your tests you can plug this in with the override_settings() decorator.
A simple test case that fails:
class FirstUrls: urlpatterns = patterns('', url(r'first/$', fake_view, name='first')) class SecondUrls: urlpatterns = patterns('', url(r'second/$', fake_view, name='second')) class OverrideSettingsTests(TestCase): """ If neither override_settings nor a settings_changed receiver clears the URL cache between tests, then one of these two test methods will fail. """ @override_settings(ROOT_URLCONF=FirstUrls) def test_first(self): reverse('first') @override_settings(ROOT_URLCONF=SecondUrls) def test_second(self): reverse('second')
In order for this to work, I think the best approach is to add another listener for the settings_changed signal in django/test/signals.py, which detects when the ROOT_URLCONF setting is changed and clears the LRU caches in django.core.urlresolvers.
Change History (10)
comment:1 Changed 3 years ago by gcc
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Type changed from Uncategorized to Bug
comment:2 Changed 3 years ago by aaugustin
- Easy pickings unset
- Patch needs improvement set
- Triage Stage changed from Unreviewed to Accepted
comment:9 Changed 2 years ago by Tim Graham <timograham@…>
- Resolution set to fixed
- Status changed from new to closed