简介

覆盖率(code coverage rate)是反映测试用例对被测软件覆盖程度的重要指标,也是衡量测试工作进展情况的重要指标。在代码逻辑比较复杂的情况下,测试工作往往只能覆盖到显而易见的逻辑分支,而更多的深层次的逻辑分支则不容易被测试人员发现。为了保证测试的覆盖率,有些开发人员会尝试协助测试人员写出所有的测试用例,这不仅会牺牲大量的宝贵的开发时间,同时也拥有一定的难度,最重要原因就是因为测试难以量化。而代码覆盖工具就是用来量化代码测试的覆盖率,让测试人员可以直观的发现那些没有覆盖到的代码分支。

OpenCppCoverageWindows平台下开源的C++代码覆盖率工具,使用简单,功能齐全而强大。

使用起来非常简单,它不需要在编译时插桩,只需要有pdb文件,运行时插桩,通过OpenCppCoverage启动进程即可。功能也比较全,主要特点有:

  • 不需要重新编译被测程序,只需要使用penCppCoverage运行程序。
  • 性能开销比较小
  • 按模块、代码路径过滤。
  • 自动生成html覆盖率结果报告
  • 支持多个覆盖率结果合并。
  • 集成Jenkins

下载与安装

OpenCppCoverage可以单独下载安装,也可以作为插件VisualStudio的进行安装。这里以单独下载安装为例。

下载

OpenCppCoverage的下载地址为:https://github.com/OpenCppCoverage/OpenCppCoverage/releases

java增量代码覆盖率统计 增量代码覆盖率工具_代码覆盖检测工具

根据自身环境选择x86或者x64版本进行安装。

安装

安装过程比较简单,只需要注意一点:勾选将运行目录添加到环境变量中(默认也是勾选)。

java增量代码覆盖率统计 增量代码覆盖率工具_OpenCppCoverage_02

使用与分析

使用

OpenCppCoverage最简单的运行格式为:
OpenCppCoverage.exe --sources MySourcePath* -- YourProgram.exe arg1 arg2

以如下的TestOpenCppCoverage工程为例,工程由utils.hmain.cpp组成:

utils.h

#include <iostream>

void Print1()
{
	std::cout << "Print1" << std::endl;
}

void Print2()
{
	std::cout << "Print2" << std::endl;
}

void Print3()
{
	std::cout << "Print3" << std::endl;
}

main.cpp

#include "utils.h"
#include <string>

int main()
{
	Print1();
	Print2();
	return 0;
}

工程的代码目录为:
C:\Users\xupeng\Desktop\TestOpenCppCoverage\TestOpenCppCoverage 工程的输出程序为:
C:\Users\xupeng\Desktop\TestOpenCppCoverage\Debug\TestOpenCppCoverage.exe

运行CMD,运行命令:
OpenCppCoverage --sources C:\Users\xupeng\Desktop\TestOpenCppCoverage\TestOpenCppCoverage -- C:\Users\xupeng\Desktop\TestOpenCppCoverage\Debug\TestOpenCppCoverage.exe

输出信息为:

[info] Start Program:
Path:"C:\\Users\\xupeng\\Desktop\\TestOpenCppCoverage\\Debug\\TestOpenCppCoverage.exe"
Arguments:
Working directory: not set.
Modules: Selected: * Excluded:
Sources: Selected: C:\Users\xupeng\Desktop\TestOpenCppCoverage\TestOpenCppCoverage Excluded:
Log Level: Normal
Cover Children: 0
Aggregate by file: 1
Continue after C++ exception: 0
Optimized build support: 0
Export: html
Input coverage:
Unified diff:
Excluded line regular expressions:
Substitute pdb source paths:
[info] Module: C:\Users\xupeng\Desktop\TestOpenCppCoverage\Debug\TestOpenCppCoverage.exe is selected because it matches selected pattern: *
[info] Module: C:\Windows\System32\ntdll.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\ntdll.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\System32\wow64.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\System32\wow64win.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\System32\wow64cpu.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\kernel32.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\KernelBase.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\ucrtbased.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\vcruntime140d.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\msvcp140d.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\advapi32.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\msvcrt.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\sechost.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\rpcrt4.dll is selected because it matches selected pattern: *
[info] Module: C:\Windows\SysWOW64\cryptbase.dll is selected because it matches selected pattern: *
Print1
Print2
[info] Module: C:\Windows\SysWOW64\kernel.appcore.dll is selected because it matches selected pattern: *
[info] ----------------------------------------------------
[info] Coverage generated in Folder C:\Users\xupeng\CoverageReport-2021-05-23-11h53m25s
[info] ----------------------------------------------------
[info] The code coverage report is not what you expect? See the FAQ https://github.com/OpenCppCoverage/OpenCppCoverage/wiki/FAQ.

输出信息说明,生成的html文件(index.html)位于:C:\Users\xupeng\CoverageReport-2021-05-23-11h53m25s


java增量代码覆盖率统计 增量代码覆盖率工具_代码覆盖检测工具_03


分析

打开index.html:

java增量代码覆盖率统计 增量代码覆盖率工具_java增量代码覆盖率统计_04

单击C:\Users\xupeng\Desktop\TestOpenCppCoverage\Debug\TestOpenCppCoverage.exe:

java增量代码覆盖率统计 增量代码覆盖率工具_OpenCppCoverage_05


再分别单击 c:\users\xupeng\desktop\testopencppcoverage\testopencppcoverage\utils.hc:\users\xupeng\desktop\testopencppcoverage\testopencppcoverage\main.cpp

即可查看utils.hmain.cpp的代码覆盖情况:


java增量代码覆盖率统计 增量代码覆盖率工具_Windows_06


java增量代码覆盖率统计 增量代码覆盖率工具_代码覆盖检测工具_07


绿色部分表示已覆盖到的部分,红色部分表示未覆盖的部分。