#28823: Nested @override_settings on class and method do not work correctly -----------------------------------------+------------------------ Reporter: Paolo D'Apice | Owner: nobody Type: Bug | Status: new Component: Uncategorized | Version: 1.10 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -----------------------------------------+------------------------ I am using the `@override_settings~ decorator to delete some settings during tests (accordint to the [https://docs.djangoproject.com/en/1.10/topics/testing/tools/#overriding- settings documentation]).
I reproduced the problem with the following code: {{{#!python from django.test import TestCase, override_settings from django.conf import settings TEST_SETTINGS = { 'MY_DATA': { 'foo': 1, 'bar': 2 } } class Dummy: def foo(self): return settings.MY_DATA.get('foo') def bar(self): return settings.MY_DATA.get('bar') @override_settings(**TEST_SETTINGS) class SimpleTest(TestCase): def setUp(self): self.dummy = Dummy() def test_foobar(self): self.assertEqual(self.dummy.foo(), 1) self.assertEqual(self.dummy.bar(), 2) @override_settings() def test_delete_my_data(self): del settings.MY_DATA with self.assertRaises(AttributeError): self.dummy.foo() with self.assertRaises(AttributeError): self.dummy.bar() @override_settings() def test_delete_foo(self): del settings.MY_DATA['foo'] self.assertIsNone(self.dummy.foo()) self.assertEqual(self.dummy.bar(), 2) def test_foobar_again(self): self.assertEqual(self.dummy.foo(), 1) self.assertEqual(self.dummy.bar(), 2) }}} I expect all the test to pass, but instead I have some failures: {{{ ..FF ====================================================================== FAIL: test_foobar (api.tests.test_bug.SimpleTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/paolo/testproject/api/tests/test_bug.py", line 26, in test_foobar self.assertEqual(self.dummy.foo(), 1) AssertionError: None != 1 ====================================================================== FAIL: test_foobar_again (api.tests.test_bug.SimpleTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/paolo/testproject/api/tests/test_bug.py", line 44, in test_foobar_again self.assertEqual(self.dummy.foo(), 1) AssertionError: None != 1 ---------------------------------------------------------------------- Ran 4 tests in 0.045s FAILED (failures=2) }}} It seems to me that the overridden settings at method level are not cleared up after the test method runs, leaving the settings in a ''dirty'' state. -- Ticket URL: <https://code.djangoproject.com/ticket/28823> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/053.ce0e0cd711217011e0905e6c88d9ea9a%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.