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并不能说明这一点。
这篇关于如何模拟测试函数使用的函数的默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何模拟测试函数使用的函数的默认参数?
基础教程推荐
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
