如何使用 vsinstr/vsperfmon 获得真实的代码覆盖率
我的基于微软的开发环境如下所示: - 巨大的原生 C++ 代码库,分为 10 个项目 - 每个项目都有一个依赖的测试项目(GoogleTest 单元测试),只需引用要测试的源。
我使用 vsinstr 和 vsperfmon(用于检测/监视可执行文件和 dll 的 Visual Studio 工具)生成了覆盖率报告,但这并不像我预期的那样令人满意,因为该报告仅显示单元测试行的覆盖率,而不显示单元测试行的覆盖率。正在测试的源(我检测了测试套件可执行文件 Sample_Project_Test.exe)。
例如,如果我有这样的方法:
(Sample_Project/add_ints.cpp)
int add(int a, int b){
return a+b;
}
int add2(int a, int b){
if (a == b)
return a * 2;
else
return a+b;
}
并且单元测试如下:
(Sample_Project_Test/int_adds_tests.cpp)
TEST(AddTest, ReturnsCorrectSum)
{
EXPECT_EQ(4, add(2,2));
}
我得到 100% 的行覆盖率,因为只测量了 add_ints.cpp 中的 add-part,add2 似乎被完全删除,因为它没有被触及。据我不明白整个覆盖范围的错误,这似乎不正确?
my microsoft-based development environment looks like this:
- huge native c++ codebase, separated into 10 projects
- each project has a dependent test project (GoogleTest unit tests), the sources to test are simply referenced.
I generated the coverage-report using vsinstr and vsperfmon (the visual studio tools for instrumenting/monitoring executables and dlls), but that wasn't as satisfying as i expected because the report shows only the coverage of the unit-test lines, not of the sources under test (I instrumented the testsuite-executable Sample_Project_Test.exe).
For example if i have a method like this:
(Sample_Project/add_ints.cpp)
int add(int a, int b){
return a+b;
}
int add2(int a, int b){
if (a == b)
return a * 2;
else
return a+b;
}
and the unit test is like this:
(Sample_Project_Test/int_adds_tests.cpp)
TEST(AddTest, ReturnsCorrectSum)
{
EXPECT_EQ(4, add(2,2));
}
I get a line coverage of 100% because ONLY the add-part in add_ints.cpp is measured, add2 seems to be completely removed because it is not touched. As far as I did not understand the whole coverage thing wrong this seems not correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用链接器选项 /OPT:NOREF 构建测试,以便它链接所有代码,而不仅仅是使用的代码。
You need to build your tests with linker option /OPT:NOREF so that it links against all code, not just the used code.
您的构建设置中是否启用了任何优化?
也许这些链接会对您有所帮助:
/GL(整体程序优化) 和 /LTCG(链接时代码生成)
Do you have any optimization enabled in your build settings?
Maybe those links would help you:
/GL (Whole Program Optimization) and /LTCG (Link-time Code Generation)