How do I mock the default arguments of a function that is used by the function I#39;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
上使用默认参数,并在我的测试中修补函数,以便将该参数修补为False
,以便它在测试期间跳过这些操作。遗憾的是,我不能使用this question中的代码,因为我正在测试main_function
,而不是_sub_function
,所以我的测试中没有_sub_function
。mock.patch.object
只能将正在修补的对象作为参数,而不是包含对象导入路径的字符串(像mock.patch
一样),因此上面的代码无法运行,它会在mock.patch.object()
行引发一个AttributeError: my_module._sub_function does not have the attribute '__defaults__'
。
是否有办法使用函数的字符串导入路径修补该函数的默认参数。
还是有更好的方法来实现我想要的?
推荐答案
唯一的问题是您试图修补str
对象的属性,而不是您的函数:
class TestMyModule(unittest.TestCase):
@mock.patch.object(my_module._sub_function, "__defaults__", (False,))
def test_main_function(self):
print(my_module.main_function())
遗憾的是,被提出的AttributeError
并不能说明这一点。
这篇关于如何模拟测试函数使用的函数的默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何模拟测试函数使用的函数的默认参数?


基础教程推荐
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01