google 测试中的存根系统函数

Stub system function in google test

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来测试它? 只有 Stub,没有 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 定义为其他内容不会改变这一点。

你不能简单地 mock a free function.

您可以更改 file() 函数以获取指向要使用的 fopen() 函数的指针。在您的测试中,您然后在调用 file() 函数时提供指向模拟函数的指针。这是一种 依赖注入 .

另一种选择是使用条件编译。

使用依赖注入的例子:

// 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`.
}

然后在你的测试代码中:

TEST(OPEN_FILE, OK)
{
    ASSERT_NE(NULL, file(&my_fopen));
}

如果您使用许多要模拟的系统函数,您还可以创建一个包含指向所有相关函数的指针的结构。

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;
}

这里的前提是如果你想测试你的代码,你需要写可测试的代码。依赖注入是实现这一目标的一种方法。