Capturing C++ Code Coverage with Visual C++

I wrote an article about six months ago on code coverage in C++, which is part of my article Agile C++ Development and Testing with Visual Studio and TFS. In that article, I showed how to use a command line to create code coverage because I was under the impression that Visual Studio 2010 didn’t support C++ code coverage. I had tried it, but couldn’t get it to work at the time.

It turns out there is a way to use capture code coverage directly in Visual Studio (or in on a TFS build server) without any customization. The trick, as it turns out, is that you have to turn on the /Profile switch in the linker.

Including ALL of your Code

If you’re using static libraries, as described in Writing Unit Tests in Visual Studio for Native C++, you will need to make a change to ensure all of your code is included. By default, the linker only includes code from static libraries that is actually used. So if you don’t have a unit test that hits some code, it won’t show up in the code coverage results. That means you won’t be able to find dead code. To fix this problem, you need to turn off linker optimizations:

image

The /OPT:NOREF switch will include all your code in the test DLL.

Turning on C++ Code Coverage

Right click on your DLL (the test DLL in my case since I use a static library to link the test library into the C++/CLI test project, see Writing Unit Tests in Visual Studio for Native C++) and turn on the /PROFILE linker switch:

image

Once you do this, you’ll be able to use the testsettings file to specify which DLLs to instrument. Double-click the testsettings file, click Data and Diagnostics, then click Code Coverage to check the box in the Enabled column:

image

Finally, click Configure, and then check the DLLs that you want to be instrumented, which in this case is the MyCodeTests.dll file that contains both the test code and the product code.

image

Excluding Test Classes from Code Coverage

If you’re using static libraries and C++/CLI for testing as I described in Writing Unit Tests in Visual Studio for Native C++, you may have noticed that your test methods also show up in the code coverage results. You can exclude all the methods in a test class from code coverage using the ExcludeFromCodeCoverageAttribute in front of the class. For example:

 [TestClass]
[ExcludeFromCodeCoverage]
public ref class MyClassFixture
{

Now all the methods in your test class will be excluded from the code coverage results.

Note: You’ll need SP1 of Visual Studio 2010 in order to see lines colored to show coverage information.

Comments

  • Anonymous
    August 16, 2011
    Hi John, This method only generates code coverage of production code (Static library) for which we have written unit test. The coverage data does not include the information of production code for which unit test has not been written. We have tried turning off code optimization with no effect on coverage data. Similarly, because of the above mention issue we are not able to determine dead production code from the coverage data of production executable. How can we determine actual amount of C++ production code tested by unit tests in VisualC++? How can we detect C++ dead production code in VisualC++? Thanks, Roshan

  • Anonymous
    August 17, 2011
    @Roshan: using the /OPT:NOREF link option will fix your problems. By default, the linker ignores code that isn't referenced. The NOREF option turns off this behavior and will therefore include all your code, allowing you to find dead code.

  • Anonymous
    August 17, 2011
    Thanks John

  • Anonymous
    November 28, 2011
    Hi John Do you know how you can use /EXCLUDE:std::* etc in the unit test process to get rid of unwanted code coverage of STL etc? Just can't see where it should go in the project settings. Thanks Steve

  • Anonymous
    March 13, 2012
    The comment has been removed

  • Anonymous
    September 10, 2013
    Hi, I am working on windows store apps using HTML & JavaScript. I am looking for code coverage tools for JavaScript. Is there any way to do it in Visual studio? External tools would be helpful too. Thanks, Ashish

  • Anonymous
    October 20, 2013
    I could not get the code coverage result in my window.

  • Anonymous
    March 22, 2015
    The comment has been removed