Opened 3 years ago

Closed 3 years ago

#21263 closed Bug (fixed)

override_settings in inherited classes should take precedence

Reported by: Sephi Owned by: nobody
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider the following test case:

@override_settings(TEST='super')
class TestSettingsOverrideInheritanceSuper(TestCase):
    def test_override_settings_inheritance(self):
        self.assertEqual(settings.TEST, 'super')


@override_settings(TEST='child')
class TestSettingsOverrideInheritanceChild(TestSettingsOverrideInheritanceSuper):
    def test_override_settings_inheritance(self):
        self.assertEqual(settings.TEST, 'child')

The expected behaviour is that the 2 tests pass. Instead, this will fail because the _pre_setup method of override_settings enables the current class settings before running the original_pre_setup method (which will enable the parent class settings).

You can easily reproduce the issue by using the code above in any test suite.

Change History (6)

comment:1 Changed 3 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted

comment:2 Changed 3 years ago by Claude Paroz

Unfortunately, it's not as simple as exchanging order of lines (original_pre_setup vs enable). If you override settings after pre_setup, settings are not overriden during that phase where for instances fixtures are loaded. Some test cases (i.e. timezone AdminTests) depend on settings being overriden during fixture loading.

comment:3 Changed 3 years ago by Sephi

That's the issue I just faced and I was about to comment on the exact same tests.

On the other hand, the current implementation also causes issues with other existing tests, we just can't see it because of the way these are written. For example you couldn't use override_settings in TestServeDisabled (in staticfiles_tests) to set DEBUG = False because it would then use the value from the base class, StaticFilesTestCase.

comment:4 Changed 3 years ago by Claude Paroz

Has patch: set

I've tried another approach to customized settings to solve this issue: https://github.com/django/django/pull/1732

comment:5 Changed 3 years ago by Marc Tamlyn

Triage Stage: AcceptedReady for checkin

Looks good to me. Tests all pass locally and code looks sane.

comment:6 Changed 3 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In 949076eb11011736f9e827adfde03c842bc1f1dd:

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.

Note: See TracTickets for help on using tickets.
Back to Top