我是python的初学者,我需要测试一个被引用调用的C函数。
这是我的C文件: myfile.c
#include<stdio.h>
int my_function(int *x)
{
int A;
printf("enter value of A");
scanf("%d",&A);
*x = 10 * A; // this sets the value to the address x is referencing to
return 0;
}现在,我的python脚本应该调用my_function()并传递参数,以便我可以检查和验证结果。
类似于:
result = self.loaded_class.my_function(addressOf(some_variable))
self.assertEqual(some_variable,10)
这可能吗??我如何才能做到这一点。我正在用python编写脚本来进行自动测试,而不是使用交互式python。
发布于 2013-03-19 22:04:13
如果你把你的文件编译成一个共享库或者dll (我不知道怎么做),你可以像这样使用ctypes (假设这个例子是dll ):
import ctypes as ct
mylib = ct.cdll.myfile
c_int = ct.c_int(0)
mylib.my_function(ct.byref(c_int))
print c_int.value发布于 2013-03-19 21:12:04
你可以为C函数写一个Python接口,一个简单的例子是用Python doc编写的。然而,如果你只想测试C函数,你最好使用C/C++测试框架,比如Google Test。
https://stackoverflow.com/questions/15500410
复制相似问题