Google测试中的存根系统功能 [英] Stub system function in google test
问题描述
我正在尝试使用Google Test测试C代码,但遇到一些与系统功能的写存根相关的问题,例如:fopen,fclose,fread,fwrite,memcpy,memset,stat等...知道如何正确地对它们进行存根以覆盖功能中需要测试的所有分支.
I am trying to use Google Test to test C code but I am encounter some problem related to write stub for system functions like: fopen,fclose,fread,fwrite, memcpy,memset,stat,...I don't known how to stub them correctly to cover all branchs in function that need to be tested.
示例,我有一个函数,如何通过存根fopen,fclose,fwrite,fread对其进行测试?只有存根,没有模拟.
Example , I have a function, how to test it by stub fopen, fclose, fwrite, fread? Only Stub, not Mock.
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *f;
//initialize the arr1 with values
int arr1[5]={1,2,3,4,5};
int arr2[5];
int i=0;
//open the file for write operation
if((f=fopen("includehelp.txt","w"))==NULL){
//if the file does not exist print the string
printf("Cannot open the file...");
exit(1);
}
//write the values on the file
if((fwrite(arr1,sizeof(int),5,f))!=5){
printf("File write error....\n");
}
//close the file
fclose(f);
//open the file for read operation
if((f=fopen("includehelp.txt","r"))==NULL){
//if the file does not exist print the string
printf("Cannot open the file...");
exit(1);
}
//read the values from the file and store it into the array
if((fread(arr2,sizeof(int),5,f))!=5){
printf("File write error....\n");
}
fclose(f);
printf("The array content is-\n");
for(i=0;i<5;i++){
printf("%d\n",arr2[i]);
}
return 0;
}
推荐答案
sample.c
中的 file()
函数调用 fopen()
.在完全不同的文件(编译单元)中将 fopen
定义为其他内容不会改变这一点.
Your file()
function in sample.c
calls fopen()
. Defining fopen
as something else in a totally different file (compilation unit) is not going to change that.
您不能简单地模拟一个免费功能.
您可以更改 file()
函数以获取指向要使用的 fopen()
函数的指针.然后,在测试中,当调用 file()
函数时,您将提供一个指向模拟函数的指针.这是依赖注入的一种形式.
You can change the file()
function to take a pointer to the fopen()
function to use. In your tests you then provide a pointer to your mock function when calling the file()
function. This is a form of dependency injection.
另一种选择是使用条件编译.
Another option is to use conditional compilation.
使用依赖注入的示例:
// Typedef for our "fopen interface". Makes our code a bit more readable.
typedef FILE *(*fopen_type)(const char *, const char *);
FILE *file(fopen_type fopen_func)
{
FILE *f = fopen_func("abc", "r"); // Call the provided "fopen" function.
return f; // Let's return the opened file or `NULL`.
}
然后在您的测试代码中:
And then in your test code:
TEST(OPEN_FILE, OK)
{
ASSERT_NE(NULL, file(&my_fopen));
}
如果使用许多要模拟的系统功能,则还可以创建一个结构,其中包含指向所有相关功能的指针.
If you use many system functions that you want to mock, you can also create a struct that contains pointers to all the relevant functions.
struct system_calls {
fopen_type fopen;
// Add more system calls here.
};
FILE *file(struct system_calls *p)
{
FILE *f = p->fopen("abc", "r");
return f;
}
这里的前提是,如果要测试代码,则需要编写可测试的代码.依赖注入是实现此目标的一种方法.
The premise here is that if you want to test your code, you need to write testable code. Dependency injection is one way to achieve that.
这篇关于Google测试中的存根系统功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!