如何使用GCOV计算核心C程序的测试柜的代码覆盖范围?

发布于 2025-01-17 10:48:42 字数 302 浏览 0 评论 0原文

C程序包含一个.c和.h文件。

当C程序编译并运行时,它询问命令行输入(该程序继续要求输入,直到用户输入“再见”并单击Enter)。

测试柜包括大量和.out格式文件。

.in文件是命令行的输入,.out文件是程序的输出。

我有运行.in格式文件的.sh文件,并将输出与.out格式文件进行比较。我的测试用例(.in,.out文件)位于一个称为“ tests”的文件夹中。

我发现在GCOV中使用的参数无法使用.sh格式文件。

我不知道如何计算我的测试柜的代码覆盖范围,并找到我的测试柜未涵盖哪些代码行?

The C program comprises a .c and .h files.

When the C program compiles and runs, it asks command line input(the program continues ask for input until user enter "bye" and click enter).

The testcases includes lots of .in and .out format files.

The .in file is the input of command line,.out file is the output of the program.

I have the .sh file that runs the .in format file and compare output with .out format file.My test cases(.in,.out files) are in a folder called "tests".

I found the argument that use in gcov can't use the .sh format file.

I wonder how to calculate the code coverage of my testcases and find which lines of code are not covered by my testcases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔书几行 2025-01-24 10:48:42

您可以简单地添加测试覆盖率:

  1. 在编译选项中,添加-fprofile-arcs -ftest-coverage。这样,生成的可执行程序每次调用时都会生成统计信息(在 .gcno / .gcda 文件中)。

  2. 执行.sh测试文件

  3. 要求gcov生成统计文件:输入gcov -m *.c,这将产生.c.gcov 包含覆盖率测试结果的文件。

例如,我们可以创建以下程序并测试它。

#include <stdio.h>

int main(int argc) 
{
    if (argc) {
        printf("not zero");
    } else {
        printf("zero"); 
    }
    return 0;
}
gcov -m *.c
File 'foo.c'
Lines executed:80.00% of 5
Creating 'foo.c.gcov'

结果文件将是:

        -:    0:Source:foo.c
        -:    0:Graph:foo.gcno
        -:    0:Data:foo.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <stdio.h>
        -:    2:
        1:    3:int main(int argc) 
        -:    4:{
        1:    5:    if (argc) {
        1:    6:        printf("not zero");
        -:    7:    } else {
    #####:    8:        printf("zero");
        -:    9:    }
        1:   10:    return 0;
        -:   11:}

You can add test coverage simply:

  1. In your compilation option, add -fprofile-arcs -ftest-coverage. That way, the produced executable program produce statistics each times it is called (in .gcno / .gcda files).

  2. Execute you .sh test file

  3. Ask gcov to generate statistics files: type gcov -m *.c, that will produce .c.gcov files containing the coverage test results.

For instance, We can create the following program and test it.

#include <stdio.h>

int main(int argc) 
{
    if (argc) {
        printf("not zero");
    } else {
        printf("zero"); 
    }
    return 0;
}
gcov -m *.c
File 'foo.c'
Lines executed:80.00% of 5
Creating 'foo.c.gcov'

The result file will be:

        -:    0:Source:foo.c
        -:    0:Graph:foo.gcno
        -:    0:Data:foo.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <stdio.h>
        -:    2:
        1:    3:int main(int argc) 
        -:    4:{
        1:    5:    if (argc) {
        1:    6:        printf("not zero");
        -:    7:    } else {
    #####:    8:        printf("zero");
        -:    9:    }
        1:   10:    return 0;
        -:   11:}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文