在Python的unittest框架中修改全局变量

22 浏览
0 Comments

在Python的unittest框架中修改全局变量

我正在编写一系列的Python单元测试,其中一些依赖于配置变量的值。这些变量存储在一个全局的Python配置文件中,并且在其他模块中使用。我想为不同的配置变量值编写单元测试,但是目前还没有找到方法来做到这一点。

我没有重新编写我正在测试的方法的签名的可能性。

这是我想要实现的:

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(), "第一个结果")
    def test_second_case(self):
         from config import MY_CONFIG_VARIABLE
         MY_CONFIG_VARIABLE = False
         self.assertEqual(my_function_with_global_var(), "第二个结果")

谢谢。

编辑:使示例代码更加明确。

0