How to exclude test assembly from code coverage in VS 2012

Posted by Anuraj on Monday, January 28, 2013

.Net Code coverage Visual Studio

If you are using VS 2012, by default Code coverage results are available for Test assembly also.

Test Assembly coverage also displayed

There is no direct way to disable this behavior, either you need to use the ExcludeFromCodeCoverage attribute or you need to use the runsettings file.

The ExcludeFromCodeCoverage is pretty straight forward; you need to decorate all the unit test classes with this attribute, like this

[ExcludeFromCodeCoverage]
[TestClass] 
public class MathTest
{
}

But this attribute only works in Class, Structs, Constructors, Methods, Properties and Events. So if you want to disable code coverage for an entire assembly this will not work.

The other option is using runsettings file. To use this VS feature you need to add a new file with .runsettings extension to the solution. Copy the XML content below and paste it to the .runsettings file.

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage" 
                     uri="datacollector://Microsoft/CodeCoverage/2.0" 
                     assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
        <Configuration>
          <CodeCoverage>
            <ModulePaths>
              <Include>
                <ModulePath>.*\.dll$</ModulePath>
              </Include>
              <Exclude>
                <ModulePath>.*\.test.dll</ModulePath>
              </Exclude>
            </ModulePaths>
            <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
            <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
            <CollectFromChildProcesses>True</CollectFromChildProcesses>
            <CollectAspDotNet>False</CollectAspDotNet>
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

Select the .runsettings file from Test > Test Settings > Select Test Settings file menu.

Select runsettings file

Now if you analyze the code coverage with the new runsettings file, only the code file assembly coverage will be displayed.

Only Code assembly coverage is displayed

You can disable the use of .runsettings file by un-checking it in the Test Settings Menu item.

You can find more details about ExcludeFromCodeCoverage attribute in MSDN



Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



Subscribe


Copyright © 2017 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub