linux生成覆盖率

在Linux下,可以使用Gcov工具生成覆盖率报告。Gcov是GNU的代码覆盖测试工具,它可以用来衡量测试用例对源代码的覆盖程度。

编写示例代码

下面是一个简单的示例:
假设你有一个C语言程序,源文件名为hello.c,其内容如下:

#include <stdio.h>
int main(int argc, char **argv)
{
    for(int i=0; i<5; i++)
    {
        printf("Hello World\n");
    }
    return 0;
}

增加编译选项-fprofile-arcs -ftest-coverage编译成可执行文件

[test@localhost coverage]$ gcc -fprofile-arcs -ftest-coverage hello.c -o hello
[test@localhost coverage]$ ls -al 
总用量 52
drwxrwxr-x.  2 test test 68 521 20:01 .
drwxrwxr-x. 12 test test 4096 521 19:51 ..
-rwxrwxr-x.  1 test test 33512 521 20:00 hello
-rw-rw-r--.  1 test test 124 521 19:56 hello.c
-rw-rw-r--.  1 test test 448 521 20:00 hello.gcno
-rw-rw-r--.  1 test test 412 521 19:55 Makefile

新增的编译选项将会生成gcno为后缀的文件。

运行程序

[test @localhost coverage]$ ./hello 
Hello World
Hello World
Hello World
Hello World
Hello World
[test @localhost coverage]$ ls -al 
总用量 56
drwxrwxr-x.  2 test test 86 521 20:03 .
drwxrwxr-x. 12 test test 4096 521 19:51 ..
-rwxrwxr-x.  1 test test 33512 521 20:00 hello
-rw-rw-r--.  1 test test 124 521 19:56 hello.c
-rw-rw-r--.  1 test test 184 521 20:03 hello.gcda
-rw-rw-r--.  1 test test 448 521 20:00 hello.gcno
-rw-rw-r--.  1 test test 412 521 19:55 Makefile

运行hello程序,将会生成hello.gcda文件

gcov生成out文件

[test @localhost coverage]$ gcov -b -f -p -o .  hello.c >hello.c.gcov.out
[test @localhost coverage]$ 
[test @localhost coverage]$ 
[test @localhost coverage]$ 
[test @localhost coverage]$ ll
总用量 60
-rwxrwxr-x. 1 test test 33512 521 20:00 hello
-rw-rw-r--. 1 test test 124 521 19:56 hello.c
-rw-rw-r--. 1 test test 555 521 20:07 hello.c.gcov
-rw-rw-r--. 1 test test 301 521 20:07 hello.c.gcov.out
-rw-rw-r--. 1 test test 184 521 20:03 hello.gcda
-rw-rw-r--. 1 test test 448 521 20:00 hello.gcno
-rw-rw-r--. 1 test test 412 521 19:55 Makefile

生成info

lcov -d . -c -o lcov.info

生成网页

–output-direcotry选项用于指定输出html网也的目录名
–title选项:输出网页内容
–prefix选项:输出网页的保存路径

genhtml --show-details --legend --output-directory=coverage --title=hello--num-spaces=4 --prefix=.  lcov.info
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值