Part 3 : Visual Studio Code Coverage

This post come from one of our A-Team members Kiran Kumar Grandhe

In this post we shall cover about the changes to the Code Coverage in Visual studio 2012 (VS12) from Visual Studio 2010(VS10).

In VS 10, we needed to set the code coverage setting in a .testsettings file, it is in this file where you had specified the targets dlls for code coverage. In VS 12 the experience is far more transparent, your code is instrumented as the code gets loaded by the run time. All the code / binaries that is targeted by your tests will be instrumented and code coverage results can be viewed.

In VS12 to open the Code Coverage results navigate to Test->Analyze Code Coverage -> and select the test for which you would need to test the code coverage. This is much better than having to set up flags in a .testsettings file as done in VS10.



This will start off a new run for the scripts and after the scripts are done executing; we see the results as follows. The results are shown in terms of covered blocks and can be drilled down to specific methods and clicking on the method takes you to the source code, which itself is color coded blue to show the lines are covered by the tests and red to show the code that is not covered.


A Note about .testSettings file, it is still editable in VS2012, which is where you can set the targets for code coverage, you still have the option to do it. However, it is a deprecated feature, and these targets do not work. This feature is just to help edit the file and the UI clearly shows that this setting is only for backward compatibility for VS10.


If you need to specify the Targets of Code coverage in VS12, you need to do this via a .runsettings file. This is a new file that has been introduced in VS12. This is done, because the .testSettings file only supports MSTEST.exe. Since VS12 supports other custom test runners (eg NUnit, xNunit) these settings will not be applied for those test runners. You can find more details on how to work with a .runsettings file from this msdn link here.



Finally a word about the .coveragexml files. In VS12, the coverage data is all present in one single file and has all the data that is required. It has the information required to show what blocks are covered. Hence this file is all required to share to people who have the source code to show the covered blocks (colored). It’s not necessary to share the instrumented dlls to show the coverage offline.