如果在单元测试中,则跳过 python 中的代码

Skipping code in python if in a unittest

我当前的脚本调用外部脚本来执行某些任务。我想在单元测试中检查到那时为止的代码,但实际上 运行 外部脚本。如果代码 运行 作为单元测试的一部分,有什么方法可以告诉脚本有效地跳过以下块?

一种可能的方法是在单元测试中设置一个环境变量,并在被测试的脚本中检查该环境变量。

例如,在unittest.py中:

os.environ["testing"] = "1"

script-to-be-tested.py中:

testing = os.environ["testing"]
... do stuff based on the testing variable

由于script-to-be-tested.py将从unittest.py调用,它应该继承环境变量。

可能不是最干净的解决方案,但它应该有效。

unittest 包广泛支持 "mocking" 函数和方法。将对外部程序的调用封装在一个简单的函数中,您的单元测试可以覆盖该函数 ("mock out"),而无需修改程序的结构。示例:

这是您程序的一部分,在模块 realcode.py

def internal_function_calling_exec(arg1):
    """The real thing"""
    print("I am executing an external program")


def bigger_function_being_tested(arg1, arg2):
    """
    A complex function with one or more calls to `internal_function_calling_exec`
    """
    print("I will now call `internal_function_calling_exec()`")
    internal_function_calling_exec(42)

您的单元测试可以如下所示:

import unittest
from unittest.mock import patch
import realcode

class MyTest(unittest.TestCase):

    @patch("realcode.internal_function_calling_exec")
    def test_something(self, mocked_func):
        realcode.bigger_function_being_tested(1, 2)
        mocked_func.assert_called_with(42)

这永远不会调用原来的internal_function_calling_exec()。相反,这将触发对模拟对象的调用;然后您的测试可以查询该对象以确认它被正确调用。

有一些方法可以模拟 class 方法等,因此您可以模拟 subprocess.call,例如。但我认为以上是更好的模式。