如何使VS忽略测试dll的代码覆盖率

时间:2019-01-28 13:04:36

标签: c# unit-testing visual-studio-2017 code-coverage

当前,当我运行代码覆盖率分析时,报告的覆盖率是90%。事实是,另外10%是实际测试的代码!

如何让VS忽略该测试代码,而仅考虑实际代码?

3 个答案:

答案 0 :(得分:0)

您可以在项目中添加运行设置文件。

在该文件中,您可以提及需要从代码覆盖范围中排除的DLL名称:

<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 pageThis page应该为您提供有关如何添加以及如何为单元测试配置运行设置文件的更多详细信息。

希望这对您有所帮助。

答案 1 :(得分:0)

1。您需要在测试项目中添加Xml文件,其扩展名应为.runsettings。添加此运行设置文件后,请复制以下代码段并粘贴到运行设置文件中。

2。在 ModulePaths 标记中,有 Exclude 标记。在此标记中,您可以提及需要从代码覆盖范围中排除的DLL名称或项目名称。

3。对于Test Project,我们应该提到项目名称而不是DLL。例如:我的测试项目名称为 Skyve.Helper.Document.Test 。因此,我在 Exclude 标记内提到了项目名称。

<?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>

答案 2 :(得分:-1)

现在有一个ExcludeFromCodeCoverage属性,可以用来修饰排除的代码。我一直在成功使用它。

将此属性放在类或结构上会从代码覆盖率信息的集合中排除该类或结构的所有成员。