我想知道是否可以使用Python 模拟库来模拟C函数以及如何?
要在Python中对C函数进行单元测试,我使用共享库,但我不能模拟一个函数来传递测试.当不需要使用mock时,测试工作正常.
所以我的问题是:是否可以使用Python mock模拟C函数以及如何?
这是一个源代码示例.
//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) …Run Code Online (Sandbox Code Playgroud)