是否可以使用python模拟C函数?
我想知道是否可以使用Python库模拟C函数,以及如何模拟 为了在Python中对C函数进行单元测试,我使用了共享库,但是我不能模拟一个函数来通过测试。当不需要使用mock时,测试工作正常 所以我的问题是:是否可以使用PythonMock模拟C函数,以及如何模拟 下面是一个源代码示例是否可以使用python模拟C函数?,python,c,unit-testing,mocking,pymock,Python,C,Unit Testing,Mocking,Pymock,我想知道是否可以使用Python库模拟C函数,以及如何模拟 为了在Python中对C函数进行单元测试,我使用了共享库,但是我不能模拟一个函数来通过测试。当不需要使用mock时,测试工作正常 所以我的问题是:是否可以使用PythonMock模拟C函数,以及如何模拟 下面是一个源代码示例 //read_sensor.c #include <stdint.h> #include "read_sensor.h" uint8_t read_sensor (vo
//read_sensor.c
#include <stdint.h>
#include "read_sensor.h"
uint8_t read_sensor (void)
{
return 0xff;
}
//event_report.c
include <stdint.h>
#include <string.h>
#include "event_report.h"
#include "read_sensor.h"
#define TRUE 1
#define FALSE 0
extern char status ;
extern uint8_t flag;
void read_distance(void)
{
uint8_t count;
uint8_t value;
uint8_t flag_count = 0;
for (count = 0; count < 3; count ++)
{
value = read_sensor();
if ( value < 100 )
{
flag_count++;
}
}
if ( flag_count == 3)
{
flag = TRUE;
}
else
{
flag = FALSE;
}
}
使用inspect.getmembers()
检查后,我得到以下结果:
[('_FuncPtr', <class 'ctypes._FuncPtr'>), ('__class__', <class
'ctypes.CDLL'>), ('__delattr__', <method-wrapper '__delattr__' of
CDLL object at 0xffa87eec>), ('__dict__', {'_FuncPtr': <class
'ctypes._FuncPtr'>, '_handle': 1690304512, '_name':
'd:/test_pytest/test_sensor_report/event_report.so'}),
('__doc__', "An instance of this class represents a loaded
dll/shared\n library, exporting functions using the standard C
calling\n convention (named 'cdecl' on Windows).\n\n The
exported functions can be accessed as attributes, or by\n
indexing with the function name. Examples:\n\n <obj>.qsort ->
callable object\n <obj>['qsort'] -> callable object\n\n
Calling the functions releases the Python GIL during the call
and\n reacquires it afterwards.\n "), ('__format__',
<built-in method __format__ of CDLL object at 0xffa87eec>),
('__getattr__', <bound method CDLL.__getattr__ of <CDLL
'd:/test_pytest/test_sensor_report/event_report.so', handle
64c00000 at ffa87eec>>), ('__getattribute__', <method-wrapper
'__getattribute__' of CDLL object at 0xffa87eec>),
('__getitem__', <bound method CDLL.__getitem__ of <CDLL
'd:/test_pytest/test_sensor_report/event_report.so', handle
64c00000 at ffa87eec>>), ('__hash__', <method-wrapper
'__hash__' of CDLL object at 0xffa87eec>), ('__init__',
<bound method CDLL.__init__ of <CDLL
'd:/test_pytest/test_sensor_report/event_report.so',
handle 64c00000 at ffa87eec>>), ('__module__', 'ctypes'),
('__new__', <built-in method __new__ of type object at
0x3d35b600>), ('__reduce__', <built-in method __reduce__ of
CDLL object at 0xffa87eec>), ('__reduce_ex__', <built-in
method __reduce_ex__ of CDLL object at 0xffa87eec>),
('__repr__', <bound method CDLL.__repr__ of <CDLL
'd:/test_pytest/test_sensor_report/event_report.so',
handle 64c00000 at ffa87eec>>), ('__setattr__',
<method-wrapper '__setattr__' of CDLL object at
0xffa87eec>), ('__sizeof__', <built-in method
__sizeof__ of CDLL object at 0xffa87eec>),
('__str__', <method-wrapper '__str__' of CDLL object
at 0xffa87eec>), ('__subclasshook__', <built-in
method __subclasshook__ of type object at
0xffae8204>), ('__weakref__', None),
('_func_flags_', 1), ('_func_restype_', <class
'ctypes.c_long'>), ('_handle', 1690304512),
('_name',
'd:/test_pytest/test_sensor_report/event_report.so')]
[(“函数”)、(“类”、(“类”、)、(“属性”、)、(“命令”、(“函数”):、“句柄”:1690304512、“名称”:
'd:/test_pytest/test_sensor_report/event_report.so'}),
(“文档”,此类的实例表示已加载的
dll/shared\n库,使用标准C导出函数
正在调用\n约定(在Windows上命名为“cdecl”。\n\n
导出的函数可以作为属性访问,也可以通过\n
使用函数名编制索引。示例:\n\n.qsort->
可调用对象\n['qsort']->可调用对象\n\n
调用函数会在调用过程中释放Python GIL
然后\n重新获取它。\n“,(“格式”,
),
("获取属性","获取属性",,
("获取项目","散列","初始化",,
),(“模块”、“ctypes”),
(新),(减少),(减少),(减少),(增加),,
("报告","","设置属性",,
)",("大小",),,
("结构","子类钩","无",,
(“功能标志”,1),(“功能重新类型”),(“句柄”,1690304512),
(“你的名字”,
'd:/test\u pytest/test\u sensor\u report/event\u report.so')]
如果你嘲笑它,你就假装它是可靠的。或者至少不要在使用模拟的测试中测试模拟的功能
完全模拟C函数没有意义,不需要加载SO对象
Mock self.event并向其添加Mock实例“read_sensor”,使用Mock 你能提供一段代码来看看你是如何包含你的C库的吗?我把它添加到了原始问题中,这在理论上是可能的,至少如果C函数没有声明为“静态”的话。我不确定它是否是由任何模拟框架完成的。如果您检查self.event,您会看到什么?(使用inspect模块)我将其添加到原始问题中。如果他完全模拟self.event,他不会模拟所有的c函数吗?根据您的建议,如何测试c函数?也许我不理解你。
[('_FuncPtr', <class 'ctypes._FuncPtr'>), ('__class__', <class
'ctypes.CDLL'>), ('__delattr__', <method-wrapper '__delattr__' of
CDLL object at 0xffa87eec>), ('__dict__', {'_FuncPtr': <class
'ctypes._FuncPtr'>, '_handle': 1690304512, '_name':
'd:/test_pytest/test_sensor_report/event_report.so'}),
('__doc__', "An instance of this class represents a loaded
dll/shared\n library, exporting functions using the standard C
calling\n convention (named 'cdecl' on Windows).\n\n The
exported functions can be accessed as attributes, or by\n
indexing with the function name. Examples:\n\n <obj>.qsort ->
callable object\n <obj>['qsort'] -> callable object\n\n
Calling the functions releases the Python GIL during the call
and\n reacquires it afterwards.\n "), ('__format__',
<built-in method __format__ of CDLL object at 0xffa87eec>),
('__getattr__', <bound method CDLL.__getattr__ of <CDLL
'd:/test_pytest/test_sensor_report/event_report.so', handle
64c00000 at ffa87eec>>), ('__getattribute__', <method-wrapper
'__getattribute__' of CDLL object at 0xffa87eec>),
('__getitem__', <bound method CDLL.__getitem__ of <CDLL
'd:/test_pytest/test_sensor_report/event_report.so', handle
64c00000 at ffa87eec>>), ('__hash__', <method-wrapper
'__hash__' of CDLL object at 0xffa87eec>), ('__init__',
<bound method CDLL.__init__ of <CDLL
'd:/test_pytest/test_sensor_report/event_report.so',
handle 64c00000 at ffa87eec>>), ('__module__', 'ctypes'),
('__new__', <built-in method __new__ of type object at
0x3d35b600>), ('__reduce__', <built-in method __reduce__ of
CDLL object at 0xffa87eec>), ('__reduce_ex__', <built-in
method __reduce_ex__ of CDLL object at 0xffa87eec>),
('__repr__', <bound method CDLL.__repr__ of <CDLL
'd:/test_pytest/test_sensor_report/event_report.so',
handle 64c00000 at ffa87eec>>), ('__setattr__',
<method-wrapper '__setattr__' of CDLL object at
0xffa87eec>), ('__sizeof__', <built-in method
__sizeof__ of CDLL object at 0xffa87eec>),
('__str__', <method-wrapper '__str__' of CDLL object
at 0xffa87eec>), ('__subclasshook__', <built-in
method __subclasshook__ of type object at
0xffae8204>), ('__weakref__', None),
('_func_flags_', 1), ('_func_restype_', <class
'ctypes.c_long'>), ('_handle', 1690304512),
('_name',
'd:/test_pytest/test_sensor_report/event_report.so')]