4

Currently, when I run code coverage analysis the reported coverage is 90%. The thing is that the other 10% is the code for the actual test!

How can i make VS ignore that test code and only take into account the actual code?

Leonardo
  • 10,737
  • 10
  • 62
  • 155

3 Answers3

3

You can add a runsettings file in your project.

In that file you can mention DLL names which needs to be excluded from code coverage:

<ModulePaths>
  <Include>
    <!-- Include all loaded .dll assemblies (but not .exe assemblies): -->
    <ModulePath>.*\.dll$</ModulePath>
  </Include>
  <Exclude>
    <!-- But exclude some assemblies: -->
    <ModulePath>.*\\Fabrikam\.MyTests1\.dll$</ModulePath>
    <!-- Exclude all file paths that contain "Temp": -->
    <ModulePath>.*Temp.*</ModulePath>
  </Exclude>
</ModulePaths>

This page and This page should give you more details on how to add and how to configure runsettings file for unit tests.

Hope this helps you.

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
  • In case you are wondering where the file is located then go to Test Menu -> Test Settings and you will see a .runsettings file there. If not, a new one can be created as per the sample on Microsoft docs here:https://learn.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2019 – Horizon Aug 09 '19 at 06:33
  • @Manoj_Choudhari It's not working in my case https://stackoverflow.com/questions/62710466/runsettings-xml-file-not-working-with-dotnet-test-coverage-command – Manish Jain Jul 03 '20 at 09:41
0

1.You need to add Xml file in your Test project which extension should be .runsettings. once you add this runsettings file,copy the below code snippet and paste in runsettings file.

2.Inside ModulePaths tag , there is Exclude tag.In this tag you can mention DLL names or project name which needs to be excluded from code coverage.

3.For Test Project , we should mention project name rather than it's DLL. For example : My test Project name is Skyve.Helper.Document.Test . So I Mentioned Project name inside the Exclude tag.

<?xml version="1.0" encoding="utf-8"?>
<!-- File name extension must be .runsettings -->
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
  <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <Configuration>
      <CodeCoverage>
        <ModulePaths>
          <Include>
          </Include>
           <Exclude>
             <ModulePath>.*CPPUnitTestFramework.*</ModulePath>
             <ModulePath>.*TestAdapter.*</ModulePath>
             <ModulePath>.*\moq.dll$</ModulePath>
             <ModulePath>.*Skyve.Helper.Document.Test.*</ModulePath>

          </Exclude>
        </ModulePaths>
        <!-- Match fully qualified names of functions: -->
        <!-- (Use "\." to delimit namespaces in C# or Visual Basic, "::" in C++.)  -->
        <Functions>
          <Exclude>
            <Function>^Fabrikam\.UnitTest\..*</Function>
            <Function>^std::.*</Function>
            <Function>^ATL::.*</Function>
            <Function>.*::__GetTestMethodInfo.*</Function>
            <Function>^Microsoft::VisualStudio::CppCodeCoverageFramework::.*</Function>
            <Function>^Microsoft::VisualStudio::CppUnitTestFramework::.*</Function>
            <Function>.*get_.*</Function>
            <Function>.*set_.*</Function>
            <Function>.*MoveNext.*</Function>
            <!--<Function>.*ValidateAVSRequestforHierarchy.*</Function>
            <Function>.*FetchDistinctAddress.*</Function>-->

         </Exclude>
        </Functions>
        <!-- Match attributes on any code element: -->
        <Attributes>
          <Exclude>
            <!-- Don’t forget "Attribute" at the end of the name -->
            <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>
            <Attribute>^NUnit.Framework.TestFixtureAttribute$</Attribute>
            <Attribute>^Xunit.FactAttribute$</Attribute>
            <Attribute>^Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute$</Attribute>
            <!--<Attribute>^skyve.helper.Avs.Core.Proxy$</Attribute>-->
          </Exclude>
        </Attributes>
        <!-- Match the path of the source files in which each method is defined: -->
        <Sources>
          <Exclude>
            <Source>.*\\atlmfc\\.*</Source>
            <Source>.*\\vctools\\.*</Source>
            <Source>.*\\public\\sdk\\.*</Source>
            <Source>.*\\microsoft sdks\\.*</Source>
            <Source>.*\\vc\\include\\.*</Source>
            <Source>.*\\Program.cs </Source>
            <Source>.*\\Startup.cs </Source>
            <Source>.*\\Filter\\.*</Source>
            <Source>.*\\RouteConfig.cs </Source>
          </Exclude>
        </Sources>
        <!-- Match the company name property in the assembly: -->
        <CompanyNames>
          <Exclude>
            <CompanyName>.*microsoft.*</CompanyName>
          </Exclude>
        </CompanyNames>
        <!-- Match the public key token of a signed assembly: -->
        <PublicKeyTokens>
          <!-- Exclude Visual Studio extensions: -->
          <Exclude>

          </Exclude>
        </PublicKeyTokens>
        <!-- We recommend you do not change the following values: -->
        <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
        <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
        <CollectFromChildProcesses>True</CollectFromChildProcesses>
        <CollectAspDotNet>False</CollectAspDotNet>
      </CodeCoverage>
    </Configuration>
  </DataCollector>
</DataCollectors>

  • It's not working in my case https://stackoverflow.com/questions/62710466/runsettings-xml-file-not-working-with-dotnet-test-coverage-command – Manish Jain Jul 03 '20 at 09:41
  • The `.*\moq.dll$` entry should either be `.*moq.dll$` or `.*\\moq.dll$` because it does not make sense to escape the m character in an regexp. – hlovdal Dec 11 '20 at 09:56
-1

There is now the ExcludeFromCodeCoverage attribute with which excluded code can be decorated. I've been using it with great success.

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.

No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43