diff --git a/django/test/signals.py b/django/test/signals.py index 4af408ea9d..8086d50b32 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -97,4 +97,7 @@ def file_storage_changed(**kwargs): @receiver(setting_changed) def complex_setting_changed(**kwargs): if kwargs['enter'] and kwargs['setting'] in COMPLEX_OVERRIDE_SETTINGS: - warnings.warn("Overriding setting %s can lead to unexpected behaviour." % kwargs['setting']) + # Considering the current implementation of the signals framework, + # stacklevel=5 shows the line containing the override_settings call. + warnings.warn("Overriding setting %s can lead to unexpected behaviour." + % kwargs['setting'], stacklevel=5) diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py index 3597982457..b148887e34 100644 --- a/tests/settings_tests/tests.py +++ b/tests/settings_tests/tests.py @@ -227,13 +227,13 @@ class TestComplexSettingOverride(TestCase): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - override = override_settings(TEST_WARN='override') - override.enable() - self.assertEqual('override', settings.TEST_WARN) - override.disable() + with override_settings(TEST_WARN='override'): + self.assertEqual(settings.TEST_WARN, 'override') self.assertEqual(len(w), 1) - self.assertEqual('Overriding setting TEST_WARN can lead to unexpected behaviour.', str(w[-1].message)) + self.assertEqual(w[0].filename, __file__) + self.assertEqual(str(w[0].message), + 'Overriding setting TEST_WARN can lead to unexpected behaviour.') class UniqueSettingsTests(TestCase):