I am working on a series of unit tests in Python, some of which depend on the value of a configuration variable. These variables are stored in a global Python config file and are used in other modules. I would like to write unit tests for different values of the configuration variables but have not yet found a way to do this.

I do not have the possibility to rewrite the signatures of the methods I'm testing.

This is what I would like to achieve:

from my_module import my_function_with_global_var

class TestSomething(self.unittest):

    def test_first_case(self):
         from config import MY_CONFIG_VARIABLE
         MY_CONFIG_VARIABLE = True
         self.assertEqual(my_function_with_global_var(), "First result")

    def test_second_case(self):
         from config import MY_CONFIG_VARIABLE
         MY_CONFIG_VARIABLE = False
         self.assertEqual(my_function_with_global_var(), "Second result")

Thanks.

Edit: Made the example code more explicite.

share|improve this question
up vote 27 down vote accepted

Don't do this:

from my_module import my_function_with_global_var

But this:

import my_module

And then you can inject MY_CONFIG_VARIABLE into the imported my_module, without changing the system under test like so:

class TestSomething(unittest.TestCase): # Fixed that for you!

    def test_first_case(self):
         my_module.MY_CONFIG_VARIABLE = True
         self.assertEqual(my_module.my_function_with_global_var(), "First result")

    def test_second_case(self):
         my_module.MY_CONFIG_VARIABLE = False
         self.assertEqual(my_module.my_function_with_global_var(), "Second result")

I did something similar in my answer to How can I simulate input to stdin for pyunit? .

share|improve this answer
1  
Awesome. This is indeed working. – badzil Jun 8 '11 at 16:59
1  
@badzil: Excellent. Now that you have unit-test coverage, you can remove the (need for the) global variable ;-) – Johnsyweb Jun 9 '11 at 0:39
1  
I wish I could. – badzil Jun 9 '11 at 17:29
7  
This approach opens you up to a subtle bug, because the value of my_module.MY_CONFIG_VARIABLE isn't reset when the test completes. If subsequent tests run in the same process use MY_CONFIG_VARIABLE without expecting it to be changed, your tests may fail or pass based on the order they're executed. – ForeverWintr Aug 29 '16 at 23:38
3  
You're right. This is subtle but major drawback and is not the way I'd do things in 2017. I'm not sure mock.patch even shipped with Python in 2011 when I wrote this answer. – Johnsyweb Jan 2 '17 at 21:57

You probably want to mock those global variables instead. The advantage of this is that the globals get reset once you're done. Python ships with a mocking module that lets you do this.

unittest.mock.patch be used as a decorator:

class TestSomething(self.unittest):

    @patch('config.MY_CONFIG_VARIABLE', True)
    def test_first_case(self):
         self.assertEqual(my_function_with_global_var(), "First result")

You can also use it as a context manager:

    def test_first_case(self):
        with patch('config.MY_CONFIG_VARIABLE', True):
            self.assertEqual(my_function_with_global_var(), "First result")
share|improve this answer
    
Fantastic approach: simple and effective. Thanks for sharing. Specially useful with @Michele 's edit. – fedorqui Mar 9 '17 at 14:21
    
In my case I didn't care about the initial value, but needed to verify the global was written to. That's possible using with patch('config') as mock_config and self.assertEqual(True, mock_config.MY_CONFIG_VARIABLE) – codehearts Nov 11 '17 at 23:18

You code imports MY_CONFIG_VARIABLE into the local scope and then immediately overwrites the name with a different object. That won't change the value in the config module. Try

import config
config.MY_CONFIG_VARIABLE = False

instead.

share|improve this answer
1  
Maybe I forgot to mention something else. The functions I'm testing are in a separate module and therefore have already imported the global variable I need to modify. Your proposed modification only modifies the global variable in the test file. – badzil Jun 7 '11 at 16:28
    
@badzil: Probably the best option is not to use from config import MY_CONFIG_VARIABLE in the modules you are testing, but rather import config and access the variables as config.MY_CONFIG_VARIABLE. – Sven Marnach Jun 7 '11 at 17:52
    
Thanks. This works although I was looking to not modify the module I am testing. I'm guessing that as the module to test uses the import ... from ... directive, there is no way to change the value of the global variable after the import. – badzil Jun 7 '11 at 18:28
    
@badzil: If the value is immutable (like the bool in the example), than you can't change it. That's somehow the nature of immutable objects. -- Another option is to reload() and reimport all modules after changing the value in config. – Sven Marnach Jun 7 '11 at 18:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.