Unit Testing and test code coverage in C++
11:29 21 Nov 2016

I'm trying to implement a test environment in C++ and it appears to be much harder than I would expect. Seems like I have 3 options:

1) Injecting dependency through interfaces.

2) Injecting dependency through templates

A good dicussion of these options can be found here: Interfaces vs Templates for dependency injection in C++

3) Making a different executable for each test that requires a mock/stub.

Options 1 and 2 requires a lot of modification of the source code, which I'm not really a fan of. I should not have to sacrifice performance/clarity for the sake of testing. On the other hand, making different projects for each tests also doesn't seem very good.

I would be okay with the overhead that comes with option 3, but I have one big problem that I can't solve: I've not found any tools that allow me to report a valid code coverage from multiple different test executables. Merging test results never consider that the lines tested in one test could be the same line tested in another.

For example, I tried asking for help about the code coverage offered by visual studio but I found no help: Visual studio code coverage from many different unit test projects.

So, are the option 1 and 2 my only valid options to get a valid code coverage report or is there some tools I'm unaware of that would do what I want?

My question is NOT a duplicate of Code Coverage Reporting with Visual Studio 2013 Professional for native C++. I'm perfectly able to generate a code coverage for one test executable. What I'm NOT able to do is generate a cohesive report for multiple tests on multiple different projects.

EX: I have my ProjectA, with 2 different test project (TestProjectA and TestProjectB). If my ProjectA contains 10 block, visual studio code coverage will return something like 7 out of 20 blocks (instead of 10) covered because it doesn't realize TestProjectA and TestProjectB are testing the same code.

c++ visual-studio unit-testing