override_settings decoration of non-test class function

27 views
Skip to first unread message

Daniel Smith

unread,
Nov 17, 2013, 11:04:51 PM11/17/13
to django...@googlegroups.com
I recently ran into a bug in our test code due to override settings resetting the settings to default after the function returned. Obviously, we shouldn't be using the override_settings decorator on a random function, but I think there is still room for improvement. At minimum, I think it would help to provide more detail in the documentation to warn against this use case.

Here's an example demonstrating the problem, we're running Django 1.5.1 with Python 2.7:

settings.py

SETTING_A = True

foo_test.py


@override_settings(SETTING_B=False)
def foo_test_func()
  print settings.SETTING_A


@override_settings(SETTING_A=False)
class FooTest(TestCase):

  def test_foo(self):
    foo_test_func()
    foo_test_func()


The above code prints out False then True.

The only related ticket I could find is:


It seems to me that foo_test_func() should be resetting the settings to the settings it originally encountered, but that was not the effect that I saw. 

Best,
Daniel

Simon Charette

unread,
Nov 18, 2013, 11:45:41 AM11/18/13
to django...@googlegroups.com
Hi Daniel!

Prior to Django 1.6 (552a90b444) override_settings couldn't be safely nested.

Could you try running your example against this version and report back here?

I guess a 1.6 release note addition and a 1.5 warning wouldn't hurt here. Maybe you could open a ticket suggesting those two documentation addition?

Simon
Reply all
Reply to author
Forward
0 new messages