'Visual Studio's gtest unit calculates incorrect code coverage
I try to use gtest in visual studio enterprise 2022 and generate code coverage.
// pch.h
#pragma once
#include <gtest/gtest.h>
// pch.cpp
#include "pch.h"
// test.cpp
#include "pch.h"
int add(int a, int b) {
return a + b;
}
TEST(a, add) {
EXPECT_EQ(2, add(1, 1));
}
This image is my test coverage report:

Such a minimalist code. I think its code test coverage should be 100%. But in reality it's only 26.53%. I think it might be because a lot of stuff in the header file "gtest/gtest.h" is not executed. Please tell me how to write a hello world project with 100% coverage.
Solution 1:[1]
Thanks for the first two answers. I wrap my function by using namespace. and adding the .runsettings file solved the problem.
// First file
namespace qingl {
int add(int a, int b) {
return a + b;
}
}
// Second file
TEST(a, add) {
EXPECT_EQ(2, qingl::add(1, 1));
}
// Add the following under the .runsettings file
// https://docs.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2022
// .runsettings
<Functions>
<Include>
<Function>^qingl::.*</Function>
</Include>
</Functions>
Solution 2:[2]
Problem is that you do not understand what are you seeing.
If you keep production code (int add(int a, int b)) in separate source file, then test it will be easier to interpret. Note that you are interested only in coverage of production code.
Also there is a view which shows which lines and branches are covered by test by marking lines in source file. This view is easier to interpret.
Now there is code which is not covered since Catch2 has extra code which covers different scenarios: to filter test, repeat them, print reports in case of errors and so on. Since you put everything in single file this statistic covers also that. If you combine this with fact that your tested code is extremely simple (noe many lines), statistic is dominated by code coverage of test itself.
So basically this is kind of false positive.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | qingl |
| Solution 2 | Marek R |
