如何使用谷歌测试,测试的C静态函数 [英] how to test static functions of C using google test
问题描述
我有一个C文件中包含一些静态的功能,如何使用谷歌测试来测试这些静电功能?
I have a C file contains some static functions, how to use google test to test those static function?
头文件:
test.h
int accessData();
源文件:
test.c
static int value;
static int getData()
{
return value;
}
int accessData()
{
if(value != 0)
{
return getData();
}
return 0;
}
静态函数由全局函数调用,但如何使用谷歌测试,以测试这些静电功能?
static function is called by global function, but how to test those static function using google test?
推荐答案
一个实现这一目标的方法是的#include
C源文件到您的测试源。然后,静态
的功能是同一个翻译单元测试code的一部分,可以从它被称为:
One way to achieve this is to #include
the C source file into your test source. Then, the static
function is part of the same translation unit as the test code, and can be called from it:
#include "test.c"
/* here follow the tests of getData() */
这样做的缺点是,一切都在 test.c以
被再次编译,对生成时间明显影响。如果得到是一个问题,你可以考虑提取静态函数被测试到自己的源文件(如 test_p.c
,用 _p
意思是私人/内部)。然后的#includetest_p.c
来自 test.c以
和单元测试。
The downside to this is that everything in test.c
gets compiled again, with obvious impact on build times. If that gets to be a problem, you might consider extracting the static functions to be tested into their own source file (e.g. test_p.c
, with the _p
meaning private/internal). Then #include "test_p.c"
from both test.c
and your unit test.
这篇关于如何使用谷歌测试,测试的C静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!