Consider simple function:
testapp/views.py:
from django.conf import settings
def return_settings_foo():
return settings.FOO
Then in shell:
In [9]: from testapp import views
In [10]: print views.return_settings_foo()
test
In [11]:
Next we will mock settings.FOO:
In [11]: with mock.patch('testapp.views.settings.FOO', 'mocked'):
print views.return_settings_foo()
....:
mocked
So, you must mock settings module where you are calling it, (NOT where it is located) for this case it is testapp/views.
Test will be the same:
import mock
from django.test import TestCase
from testapp import views
class TestPrintFoo(TestCase):
@mock.patch('testapp.views.settings.FOO', 'mocked')
def test_print(self):
result = views.return_settings_foo()
self.assertEqual(result, 'mocked')
def test_not_mocked_print(self):
result = views.return_settings_foo()
self.assertEqual(result, 'test')
UPD
One more thing. When you use create=True on attribute that exists, no matter, if it was existed or not, it will be deleted after context end in the __exit__, you can use pdb to see that. So your FOO attr deleted after context
> /usr/local/lib/python2.7/dist-packages/mock.py(1381)__exit__()
1380 else:
-> 1381 delattr(self.target, self.attribute)
1382 if not self.create and not hasattr(self.target, self.attribute):
ipdb> self.target, self.attribute
(<django.conf.LazySettings object at 0x7f48f067db10>, 'FOO')
ipdb>
@override_settingsUPDin my answer aboutcreate=True