我是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。
答案 0 :(得分:2)
如果您将文件编译为共享库或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
答案 1 :(得分:1)
你可以为C函数编写一个Python接口,一个简单的例子在Python doc。但是,如果您只想测试C函数,那么您可能更适合使用Google Test等C / C ++测试框架。