mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #21263 -- Fixed issue with override_settings in inherited classes
When both parent and child classes are decorated with override_settings, child class settings should take precedence. Thanks Sephi for the report and Marc Tamlyn for the review.
This commit is contained in:
		| @@ -157,6 +157,7 @@ class SimpleTestCase(unittest.TestCase): | |||||||
|     # The class we'll use for the test client self.client. |     # The class we'll use for the test client self.client. | ||||||
|     # Can be overridden in derived classes. |     # Can be overridden in derived classes. | ||||||
|     client_class = Client |     client_class = Client | ||||||
|  |     _custom_settings = None | ||||||
|  |  | ||||||
|     def __call__(self, result=None): |     def __call__(self, result=None): | ||||||
|         """ |         """ | ||||||
| @@ -193,6 +194,9 @@ class SimpleTestCase(unittest.TestCase): | |||||||
|         * If the class has a 'urls' attribute, replace ROOT_URLCONF with it. |         * If the class has a 'urls' attribute, replace ROOT_URLCONF with it. | ||||||
|         * Clearing the mail test outbox. |         * Clearing the mail test outbox. | ||||||
|         """ |         """ | ||||||
|  |         if self._custom_settings: | ||||||
|  |             self._overridden = override_settings(**self._custom_settings) | ||||||
|  |             self._overridden.enable() | ||||||
|         self.client = self.client_class() |         self.client = self.client_class() | ||||||
|         self._urlconf_setup() |         self._urlconf_setup() | ||||||
|         mail.outbox = [] |         mail.outbox = [] | ||||||
| @@ -210,6 +214,8 @@ class SimpleTestCase(unittest.TestCase): | |||||||
|         * Putting back the original ROOT_URLCONF if it was changed. |         * Putting back the original ROOT_URLCONF if it was changed. | ||||||
|         """ |         """ | ||||||
|         self._urlconf_teardown() |         self._urlconf_teardown() | ||||||
|  |         if self._custom_settings: | ||||||
|  |             self._overridden.disable() | ||||||
|  |  | ||||||
|     def _urlconf_teardown(self): |     def _urlconf_teardown(self): | ||||||
|         set_urlconf(None) |         set_urlconf(None) | ||||||
|   | |||||||
| @@ -203,18 +203,11 @@ class override_settings(object): | |||||||
|                 raise Exception( |                 raise Exception( | ||||||
|                     "Only subclasses of Django SimpleTestCase can be decorated " |                     "Only subclasses of Django SimpleTestCase can be decorated " | ||||||
|                     "with override_settings") |                     "with override_settings") | ||||||
|             original_pre_setup = test_func._pre_setup |             if test_func._custom_settings: | ||||||
|             original_post_teardown = test_func._post_teardown |                 test_func._custom_settings = dict( | ||||||
|  |                     test_func._custom_settings, **self.options) | ||||||
|             def _pre_setup(innerself): |             else: | ||||||
|                 self.enable() |                 test_func._custom_settings = self.options | ||||||
|                 original_pre_setup(innerself) |  | ||||||
|  |  | ||||||
|             def _post_teardown(innerself): |  | ||||||
|                 original_post_teardown(innerself) |  | ||||||
|                 self.disable() |  | ||||||
|             test_func._pre_setup = _pre_setup |  | ||||||
|             test_func._post_teardown = _post_teardown |  | ||||||
|             return test_func |             return test_func | ||||||
|         else: |         else: | ||||||
|             @wraps(test_func) |             @wraps(test_func) | ||||||
|   | |||||||
| @@ -73,6 +73,16 @@ class ClassDecoratedTestCase(ClassDecoratedTestCaseSuper): | |||||||
|             self.fail() |             self.fail() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @override_settings(TEST='override-parent') | ||||||
|  | class ParentDecoratedTestCase(TestCase): | ||||||
|  |     pass | ||||||
|  |  | ||||||
|  | @override_settings(TEST='override-child') | ||||||
|  | class ChildDecoratedTestCase(ParentDecoratedTestCase): | ||||||
|  |     def test_override_settings_inheritance(self): | ||||||
|  |         self.assertEqual(settings.TEST, 'override-child') | ||||||
|  |  | ||||||
|  |  | ||||||
| class SettingsTests(TestCase): | class SettingsTests(TestCase): | ||||||
|     def setUp(self): |     def setUp(self): | ||||||
|         self.testvalue = None |         self.testvalue = None | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user