如何在python中模拟修补程序ctypes.windlel?
我正在尝试编写检查函数参数的单元测试:如何在python中模拟修补程序ctypes.windlel?,python,unit-testing,mocking,Python,Unit Testing,Mocking,我正在尝试编写检查函数参数的单元测试: def test_my_function(): my_function = mock.patch('mymodule.myclass.myfuction') 在mymodule中,我的功能如下所示: from ctypes import POINTER, WinDLL, c_int, cast, pointer, byref class myclass: def myfunction(): # some logic 测
def test_my_function():
my_function = mock.patch('mymodule.myclass.myfuction')
在mymodule中,我的功能如下所示:
from ctypes import POINTER, WinDLL, c_int, cast, pointer, byref
class myclass:
def myfunction():
# some logic
测试失败,并显示错误消息:
ImportError:无法导入名称Windell
因此,我试图在测试中模拟patchctypes.windell
:
mocker.patch('ctypes.WinDLL')
my_function = mock.patch('mymodule.myclass.myfuction')
出现错误:
AttributeError:没有“windell”属性
我无法模拟修补程序Windl,因为如果客户端使用Windows,则ctypes
定义Windl
。我的操作系统是Linux
有可能解决这个问题吗?我自己也找到了解决办法。默认情况下,mocker.patch
仅修补定义的属性。可以模拟不存在的属性,传递参数create=True
:
mocker.patch('ctypes.windl',create=True)