Using .runsettings to exclude assemblies from code coverage

exclude class from code coverage c# runsettings
vstest code coverage
coverlet code coverage
dotnet test runsettings
coverlet runsettings exclude
dotnet test code coverage
how to exclude a method from code coverage java
runsettings file

When running code coverage for my solution which contains multiple projects, I noticed that Code Coverage includes also my test assemblies.

I found an article which suggests the following template for .runsettings:

<?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>
                <!-- Do not specify any includes. This will attempt to include all binaries -->
              </Include>
              <Exclude>
                <!-- Exclude modules that aren't to be processed, by their name / path -->
                <ModulePath>.*Tests\.dll$</ModulePath> <!-- I put it here -->
              </Exclude>
            </ModulePaths>

            <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
            <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
            <CollectFromChildProcesses>True</CollectFromChildProcesses>
            <CollectAspDotNet>False</CollectAspDotNet>
            <!--
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.

<SymbolSearchPaths>             
  <Path>C:\Users\User\Documents\Visual Studio 11\Projects\ProjectX\bin\Debug</Path>
  <Path>\\mybuildshare\builds\ProjectX</Path>
</SymbolSearchPaths>
-->
            <Functions>
              <Exclude>
                <Function>^std::.*</Function>
                <Function>^ATL::.*</Function>
                <Function>.*::__GetTestMethodInfo.*</Function>
                <Function>^Microsoft::VisualStudio::CppCodeCoverageFramework::.*</Function>
                <Function>^Microsoft::VisualStudio::CppUnitTestFramework::.*</Function>
                <Function>.*::YOU_CAN_ONLY_DESIGNATE_ONE_.*</Function>
              </Exclude>
            </Functions>
            <Attributes>
              <Exclude>
                <Attribute>^System.Diagnostics.DebuggerHiddenAttribute$</Attribute>
                <Attribute>^System.Diagnostics.DebuggerNonUserCodeAttribute$</Attribute>
                <Attribute>^System.Runtime.CompilerServices.CompilerGeneratedAttribute$</Attribute>
                <Attribute>^System.CodeDom.Compiler.GeneratedCodeAttribute$</Attribute>
                <Attribute>^System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute$</Attribute>
              </Exclude>
            </Attributes>
            <Sources>
              <Exclude>
                <Source>.*\\atlmfc\\.*</Source>
                <Source>.*\\vctools\\.*</Source>
                <Source>.*\\public\\sdk\\.*</Source>
                <Source>.*\\microsoft sdks\\.*</Source>
                <Source>.*\\vc\\include\\.*</Source>
              </Exclude>
            </Sources>
            <CompanyNames>
              <Exclude>
                <CompanyName>.*microsoft.*</CompanyName>
              </Exclude>
            </CompanyNames>
            <PublicKeyTokens>
              <Exclude>
                <PublicKeyToken>^B77A5C561934E089$</PublicKeyToken>
                <PublicKeyToken>^B03F5F7F11D50A3A$</PublicKeyToken>
                <PublicKeyToken>^31BF3856AD364E35$</PublicKeyToken>
                <PublicKeyToken>^89845DCD8080CC91$</PublicKeyToken>
                <PublicKeyToken>^71E9BCE111E9429C$</PublicKeyToken>
                <PublicKeyToken>^8F50407C4E9E73B6$</PublicKeyToken>
                <PublicKeyToken>^E361AF139669C375$</PublicKeyToken>
              </Exclude>
            </PublicKeyTokens>
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

Now the problem is that as soon as I check my .runsettings file in the TEST\Test settings menu, I see Code Coverage analyzes only one of many assemblies in my solution.

Even if I remove the line

 <ModulePath>.*Tests\.dll$</ModulePath> <!-- I put it here -->

only one assembly is analyzed. I would like to avoid adding all my assemblies to the Include list, I just need to exclude all the test assemblies.

Why does .runsettings make code coverage see only one of the assemblies in my VS solution?

The issue is the period. For some reason the RegEx is choking on that. You can get around it by escaping the period as follows:

<ModulePaths>
    <Include>
        <ModulePath>.*MyCompany\.Namespace\.Project\.dll$</ModulePath>
    </Include>
    <Exclude>
        <ModulePath>.*ThirdParty\.Namespace\.Project\.dll$</ModulePath>
    </Exclude>
</ModulePaths>

Also, the empty includes are valid and imply that all Modules are included. The comments of the Microsoft provided sample state that:

<!--
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).
See http://msdn.microsoft.com/library/2k3te2cs.aspx.
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.
-->

Using .runsettings to exclude assemblies from code coverage , The issue is the period. For some reason the RegEx is choking on that. You can get around it by escaping the period as follows: To exclude test code from the code coverage results and only include application code, add the ExcludeFromCodeCoverageAttribute attribute to your test class. To include assemblies that aren't part of your solution, obtain the .pdb files for these assemblies and copy them into the same folder as the assembly .dll files.

On a related note, I ran into this post because I was bit thinking clearly about the regular expression nature of the include and exclude paths. For my WPF application, I wanted to exclude coverage analysis on Caliburn.Micro. So I had written

<ModulePath>Caliburn.Micro.dll</ModulePath>

Clearly, the period is messing me up. This question does not suffer from that problem, but I bet I’m not the only one to overlook this simple fact. For any other readers, please also take note that * is not a wildcard – it is the regular expression "any number of" operator. You do not want *.Caliburn, but rather .*Caliburn Thus this simple statement solved my problem:

<ModulePath>.*Caliburn.*</ModulePath>

Because it is looking for a path, not just a module name, you need the .* in front of the module to ignore it – that is, you want to ignore it at any given file path.

Excluding assemblies from Code Coverage, I regularly run Visual Studio's code coverage analysis to get an idea of your solution name with the .runsettings extension (the name needn't  We then need to exclude the tests from the code coverage results, and there are two ways to do this. 1) Add an ExcludeFromCodeCoverage attribute to the test classes. 2) Add and enable a runsettings file to the solution. Option 1) will be tedious to use when the number of tests goes up, so we will go for Option 2).

As I couldn't find this answer anywhere else, and this just took me a while to figure out, ModulePath is the full path, and you may be matching your pattern somewhere else in the path.

For example, if you have a project Foo and a project Foo.Tests, and they are built to their own directories, you will end up with Foo.Tests\bin\Release\Foo.dll and Foo.Tests\bin\Release\Foo.Tests.dll. This is the dll that the test assembly will reference, so this is the path that is used. Foo\bin\Release\Foo.dll is not directly referenced by the test assembly.

If you try to exclude .*tests.* it will match both paths and produce no coverage.

To only exclude assemblies with "test" in their file name, ignoring their path, I used

<Exclude>
  <ModulePath>.*\\[^\\]*test[^\\]*\.dll</ModulePath>
</Exclude>

How to exclude code from Code Coverage in , How to exclude code from Code Coverage in Visual Studio unit testing using 2) Add and enable a runsettings file to the solution. Option 1) Most often your test code is placed in projects and assemblies suffixed with “Test”. I'm trying to exclude all namespaces but Acme.Foundations.Web.Ext.Controllers from assembly Acme.Foundations.web.ext.dll. Please help create a runsettings file.

Unfortunately I couldn't get the other answers working for me, but I got this working, as per https://msdn.microsoft.com/en-us/library/jj159530.aspx:

<ModulePath>.*\\MyProject\.Test\.dll$</ModulePath>

How to exclude test assembly from code coverage in VS 2012, 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  Seems like you are able to exclude modules in code coverage now. Closing the thread. Feel free to open another issue if needed.

I had all sorts of issues getting <ModulePaths> to work reliably (using ASP.NET Core 2.1).

In the end I found that using the <Sources> was simple and more reliable and worked exactly as I needed. You still leverage the advice on the use of regex.

I include my solution path, and exclude my \tests subfolders where all my test projects live. Example for CodeCoverage element in RunSettings xml file:

<CodeCoverage>
  <Sources>
    <Include>
      <Source>.*\\My\.Solution\.Name\.Space\\.*</Source> <!-- ie: include *\My.Solution.Name.Space\* -->
    </Include>
    <Exclude>
      <Source>.*\\My\.Solution\.Name\.Space\\Tests\\.*</Source> <!-- ie: exclude *\My.Solution.Name.Space\Tests\* -->
    </Exclude>
  </Sources>
  <!-- removed for brevity -->
<CodeCoverage>

See more at: Customising Code Coverage on MS Docs

Is there a good example of excluding modules from code coverage , I have seen examples of using a .runsettings file to exclude modules, but when of excluding modules from code coverage using vstest.console.exe only? FileNotFoundException: Could not load file or assembly 'Microsoft. There is a solution as described in the post Customizing Code Coverage Analysis which allows us to create solution wide file to exclude assemblies from code coverage, I’m going to summarize the steps to create the file here…. Creating the .runsettings file.

project-system/CodeCoverage.runsettings at master · dotnet/project , Included items must then not match any entries in the exclude list to remain included. -->. <!-- Match assembly file paths: -->. 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.

Using .runsettings to exclude assemblies from code coverage, When running code coverage for my solution which contains multiple projects, I noticed that Code Coverage includes also my test assemblies. I found an article  A common use of a .runsettings file is to customize code coverage analysis. Specify a run settings file Run settings files can be used to configure tests that are run from the command line , in the IDE, or in a build workflow using Azure Test Plans or Team Foundation Server (TFS).

How to exclude code from Code Coverage in Visual Studio unit , We then need to exclude the tests from the code coverage results, and there are two 2) Add and enable a runsettings file to the solution. Most often your test code is placed in projects and assemblies suffixed with “Test”. Placing this attribute on a class or a structure excludes all the members of that class or structure from the collection of code coverage information. Exclude From

Comments
  • msdn.microsoft.com/de-de/library/vstudio/jj635153.aspx says that you can remove every entry in this file (as default values are used). Try to start with an empty file and add one region after the other.
  • I'm facing a similar issue, with a much simpler file. With the default runsettings file, I'm able to get code coverage. When I tried to exclude a third party library (for example), then running the Code Coverage analysis results in ... nothing. No errors, no warnings, just nothing. Comment that exclusion out, and everything works again.
  • simply adding the english (en-us) version of the MSDN link supplied by @habakuk msdn.microsoft.com/en-us/library/vstudio/jj635153.aspx
  • +1 for adding the Microsoft comment that explains exactly how the Include and Exclude work. Thank you.
  • Your solution is the correct one. It is not a simple wildcard or regex but is a Path so the .* is kind of mandatory otherwise you should know the full path of the .dll which is impossible of TFS Azure.
  • "If you try to exclude .*tests.* it will match both paths and produce no coverage." It had exactly this problem. Your answer saved my time.