我的模块中有一个未初始化的全局变量,该变量在应用程序启动期间已正确初始化。对于类型检查,我使用var: Type
没有来自 python >= 3.6 的值的语法,因此我不必使默认值的类型检查复杂化(这将是None
)。
现在我想对另一个使用这个全局变量的函数进行单元测试,但是我从unittest.mock.patch
. 这是我正在做的简化版本:
- 文件 mod.py:
global_var: bool
#global_var: bool = False
def init(val: bool) -> None:
global global_var
global_var = val
def do_stuff() -> str:
return "a" if global_var else "b"
- 文件 test.py:
import unittest.mock, mod
class MockUninitializedGlobal(unittest.TestCase):
def test_it(self):
with unittest.mock.patch("mod.global_var", True):
actual = mod.do_stuff()
expected = "a"
self.assertEqual(expected, actual)
- 我用
python3 -m unittest test.py
.
例外是
Traceback (most recent call last):
File "/home/luc/soquestion/test.py", line 4, in test_it
with mock.patch("mod.global_var", True):
File "/nix/store/vs4vj1yzqj1bkcqkf3b6sxm6jfy1gb4j-python3-3.7.7/lib/python3.7/unittest/mock.py", line 1323, in __enter__
original, local = self.get_original()
File "/nix/store/vs4vj1yzqj1bkcqkf3b6sxm6jfy1gb4j-python3-3.7.7/lib/python3.7/unittest/mock.py", line 1297, in get_original
"%s does not have the attribute %r" % (target, name)
AttributeError: <module 'mod' from '/home/luc/soquestion/mod.py'> does not have the attribute 'global_var'
如果我在 mod.py 中注释第 1 行并取消注释第 2 行,则测试通过。
有没有办法让测试通过而无需在我的应用程序代码中为全局变量定义默认值?
编辑:如评论中所述,实际变量是一个解析的配置文件,我不想在测试期间加载它。它用于命令行应用程序,因此始终在命令行解析完成后设置变量。真正的代码在这里和这里。在实际测试中,我不是直接将全局变量模拟为布尔值,而是模拟配置对象上的某些属性。