dimanche 1 mars 2015

Overriding Settings for unit tests in Django doesn't work properly

I tried to unit test my Django program and I would like to check several values for a few variables in my settings.py file.


In the documentation, the section "Overriding settings" is describing a way to do it. But, despite all my attempts, I miserably fail to make the changes available in the program while the test is running. Here is a summary of what I do on a minimal example:


File settings.py



...

TEST_VALUE = 'a'


File tests.py



from django.test import TestCase
from testing_settings.settings import TEST_VALUE

class CheckSettings(TestCase):

def test_settings(self):
self.assertEqual(TEST_VALUE, 'a')

def test_modified_settings(self):
with self.settings(TEST_VALUE='b'):
self.assertEqual(TEST_VALUE, 'b')


When I run the tests, I get the following:



$ ./manage.py test
Creating test database for alias 'default'...
F.
======================================================================
FAIL: test_modified_settings (testing_settings.tests.CheckSettings)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tests/testing_settings/tests.py", line 12, in test_modified_settings
self.assertEqual(TEST_VALUE, 'b')
AssertionError: 'a' != 'b'
----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)
Destroying test database for alias 'default'...


As you may have noticed, the original value of TEST_VALUE is 'a', and I try to modify it through the self.settings(TEST_VALUE='b')... but, without success.


You may try it through an empty project (Django is 1.6.5).


So, what am I missing to get it work properly ?


Aucun commentaire:

Enregistrer un commentaire