我有一个包含一些静态函数的C文件,如何使用Google Test来测试那些静态函数?
头文件:
test.h
int accessData();
源文件:
test.c
static int value;
static int getData()
{
return value;
}
int accessData()
{
if(value != 0)
{
return getData();
}
return 0;
}
静态函数由全局函数调用,但是如何使用Google测试来测试那些静态函数呢?
实现此目的的一种方法是#include
将C源文件放入您的测试源中。然后,该static
函数与测试代码是同一转换单元的一部分,可以从中调用该函数:
#include "test.c"
/* here follow the tests of getData() */
不利的一面是,其中的所有内容test.c
都会重新编译,这对构建时间有明显的影响。如果这成为问题,则可以考虑将要测试的静态函数提取到它们自己的源文件中(例如test_p.c
,具有_p
private / internal的含义)。然后#include "test_p.c"
从两者test.c
和您的单元测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句