这里被测试文件为:example.cpp,测试文件为test.cpp
首先进入代码所在目录,打开命令行:
1. 编译
clang++ -std=c++17 -fprofile-instr-generate -fcoverage-mapping example.cpp test.cpp -lgtest -lgtest_main -pthread -o test
2. 生成覆盖率数据
LLVM_PROFILE_FILE="test.profraw" ./test
3. 合并数据为可读格式
llvm-profdata merge -sparse test.profraw -o test.profdata
4. 终端直接查看源码级覆盖率(包含MC\DC覆盖率)
llvm-cov show ./test -instr-profile=test.profdata -show-branches=count --show-mcdc-summary
5. 终端直接查看文件级覆盖率
llvm-cov report ./test -instr-profile=test.profdata
6. 生成可视化报告
llvm-cov show -format=html -output-dir=coverage_report ./test -instr-profile=test.profdata --show-mcdc-summary
随后会在代码目录下有生成coverage_report文件夹,打开里面的index.html即可查看。