我正在编写 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(), "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")
谢谢。
编辑:使示例代码更加明确。
with patch('config') as mock_config
和self.assertEqual(True, mock_config.MY_CONFIG_VARIABLE)
实现。 - codeheartsdef my_function_with_global_var(s=MY_CONFIG_VARIABLE)
。 - sshow