0

我的模块中有一个未初始化的全局变量,该变量在应用程序启动期间已正确初始化。对于类型检查,我使用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 行,则测试通过。

有没有办法让测试通过而无需在我的应用程序代码中为全局变量定义默认值?


编辑:如评论中所述,实际变量是一个解析的配置文件,我不想在测试期间加载它。它用于命令行应用程序,因此始终在命令行解析完成后设置变量。真正的代码在这里这里。在实际测试中,我不是直接将全局变量模拟为布尔值,而是模拟配置对象上的某些属性。
4

1 回答 1

0

我使用类上的setUpandtearDown方法unittest.TestCase来解决这个问题。

通过我上面关于它实际上是一个配置对象的编辑,代码如下所示:

class Config:
    def __init__(self, filename: str):
        "Load config file and set self.stuff"
        self.stuff = _code_to_parse(filename)
config: Config
def do_stuff() -> str:
    return "a" if config.stuff else "b"

并且测试在测试之前创建一个模拟的配置对象,以便在测试期间模拟配置对象上的某些属性。在拆解中,我还再次删除了对象,这样就不会溢出到下一个测试用例。

然后,我需要的属性with的实际模拟在实际测试中的一个块中完成,因为它 (a) 更清晰,并且 (b) 我可以模拟我需要的东西,而不是模拟setUp方法中配置对象上的所有属性。

import unittest, unittest.mock, mod
class MockUninitializedGlobal(unittest.TestCase):
    def setUp(self):
        mod.config = unittest.mock.Mock(spec=mod.Config)
    def tearDown(self):
        del mod.config
    def test_it(self):
        with unittest.mock.patch("mod.config.stuff", True):
            actual = mod.do_stuff()
        expected = "a"
        self.assertEqual(expected, actual)
于 2020-07-21T17:34:43.473 回答