Like Unit Testing, Code Coverage has undergone significant change in VS 2012. Mostly the changes have simplified the process from VS 2010. Want to see code coverage now? How about right click the test and select Analyze Code Coverage. Sure beats what you had to do in VS 2010.
NB This information is compiled from two Microsoft blogs plus my own tinkering. The blogs have more detail than explained here. Check them out at
Let’s get this one out of the way quick. Code Coverage is available in Ultimate and Premium editions. Moving on…
So how does the new coverage tool know what to include or exclude? There used to be the TestSettings file that was used to specify what to include / exclude.
The new tool includes binaries “as long as these binaries are a part of your solution, and get called by any of your test cases under execution”
Ok makes sense. But what about binaries that are not part of my solution? What if I have a file reference to a different binary. Well that won’t show up. If you want to see that included in your code coverage stats then you need to do some work.
VS2012 has a new file for this called a .runsettingsfile. This is an xml file that contains which binaries to include or exclude. In RC you will need to add a new xml file to your solution and name it something.runSettings. (extension is important here) The TestSettings file is not used anymore as that was bound to MSTest and would not support the new test runner plug in architecture.
You can then copy some boilerplate text into the xml file (I am sure that the tooling will be sorted by RTM), adjust what you want to include / exclude. Finally tell you solution to use the runsetttings file and hey presto, all should be good.
Putting it all together
So lets have a go. Suppose I have the following structure.
UnitTestProject A that has a project reference to SUT.dll. These two are in the same solution so all good so far. However SUT.dll has a file reference to Account.dll. Runing code coverage from UnitTestProject A gives me.
As you can see we only get the code coverage for UnitTestProject A and the SUT.dll. This is because they are in the same solution.
So we now right click our solution, add new item, xml file. Call it CodeCoverage.runsettings (first part of the name doesn’t matter)
Now add the following xml and save.
<?xml version="1.0" encoding="utf-8"?>
<DataCollector friendlyName="Code Coverage"uri="datacollector://Microsoft/CodeCoverage/2.0"assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=184.108.40.206, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
About include/exclude lists:
Empty "Include" clauses imply all; empty "Exclude" clauses imply none.
Each element in the list is a regular expression (ECMAScript syntax).
An item must first match at least one entry in the include list to be included.
Included items must then not match any entries in the exclude list to remain included.
It is considered an error to exclude all items from instrumentation as no data would be collected.
Additional paths to search for symbol files. Symbols must be found for modules to be instrumented.
If symbols are alongside the binaries, they are automatically picked up. Otherwise specify the here.
Note that searching for symbols increases code coverage runtime. So keep this small and local.
<Path>C:\Users\User\Documents\Visual Studio 11\Projects\ProjectX\bin\Debug</Path>
This is now a default runSettings file. Next we need to make sure that our extra binary gets included. To do this we can make sure that our Include section is empty (include everything.) Change the include section to look like
Finally go to the Test menu, select TestSettings, select TestSettings file. Select your runsettings file and re-run the code coverage. We now see
All of our binaries are included!