如何模拟我正在测试的函数使用的函数的默认参数?

How do I mock the default arguments of a function that is used by the function I'm testing?

我有这个my_module.py:

def _sub_function(do_the_thing=True):
    if do_the_thing:
        do_stuff()
    else:
        do_something_else()


def main_function():
    # do some stuff
    if some_condition:
        return _sub_function()
    else:
        return _sub_function(do_the_thing=False)

那我有这个测试,test_my_module.py:

import unittest
from unittest import mock

import my_module


class TestMyModule(unittest.TestCase):
    @mock.patch.object("my_module._sub_function", "__defaults__", (False,))
    def test_main_function(self):
        print(my_module.main_function())


if __name__ == "__main__":
    unittest.main()

我有一个函数 _sub_function,它接受一个默认参数来决定它是否执行某些步骤。通常,main_function 计算何时需要执行这些操作并覆盖该默认参数。不幸的是,当 运行 测试时,我无法在通常需要的时候执行这些操作。

所以我的想法是在 _sub_function 上使用默认参数,并在我的测试中将函数修补为 monkey-patch 该参数为 False 以便它在测试期间跳过这些操作。不幸的是,我无法使用 this question 中的代码,因为我正在测试 main_function,而不是 _sub_function,所以我的测试中没有 _sub_functionmock.patch.object 只能将被修补的对象作为参数,而不是包含对象导入路径的字符串(像 mock.patch 那样),所以上面的代码不起作用,它引发了一个 AttributeError: my_module._sub_function does not have the attribute '__defaults__'mock.patch.object() 行。

有没有办法使用函数的字符串导入路径来修补函数的默认参数。

或者有更好的方法来实现我想要的吗?

唯一的问题是您试图修补 str 对象的属性,而不是您的函数:

class TestMyModule(unittest.TestCase):
    @mock.patch.object(<b>my_module._sub_function</b>, "__defaults__", (False,))
    def test_main_function(self):
        print(my_module.main_function())

不幸的是,提出的AttributeError并没有说清楚。