2

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?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
lakai
  • 209
  • 1
  • 4
  • 14
  • Further investigations showed that also included but not invoked classes are not included into the coverage report, too. Maybe I missed a command line option to include them, too? – lakai Sep 18 '09 at 11:26
  • 1
    BTW: question should probably be more like "How to get _real_ code coverage using vsinstr/vsperfmon" – Dmitry Sep 18 '09 at 15:10
  • For me, I noticed the difference between Debug/Release configurations, where Release config gave me weird results. Also be sure you excersise the instrumented executable with logic, you don't have to instrumet unit tests, just run them while vsperfmon is running to get coverage data – Robert Sep 03 '13 at 20:49

2 Answers2

2

You need to build your tests with linker option /OPT:NOREF so that it links against all code, not just the used code.

thelsdj
  • 9,034
  • 10
  • 45
  • 58
0

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)

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
Dmitry
  • 6,590
  • 2
  • 26
  • 19
  • thanks for that hint but enabling/disabling the flags didn't change anything. I think that points in the right direction, the code seems either to be not listed because its percentage is 0% or 'removed' from the executeable so that it can't be instrumented. – lakai Sep 21 '09 at 06:45