How to exclude test assembly from code coverage in VS 2012

January 28, 2013 by Anuraj

.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

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"?>
      <DataCollector friendlyName="Code Coverage" 
                     assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">

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

Copyright © 2024 Anuraj. 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. Powered by Jekyll. Hosted with ❤ by GitHub