【问题标题】:How to make VS ignore code coverage for Test dll如何让 VS 忽略测试 dll 的代码覆盖率
【发布时间】:2020-02-18 12:28:50
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    在该文件中,您可以提及需要从代码覆盖范围中排除的 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 应该为您提供有关如何添加以及如何为单元测试配置 runsettings 文件的更多详细信息。

    希望这对您有所帮助。

    【讨论】:

    【解决方案2】:

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

    2.在ModulePaths标签内,有Exclude标签。在这个标签中可以提及需要排除在代码覆盖范围之外的DLL名称或项目名称。 p>

    3.对于测试项目,我们应该提到项目名称而不是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>
    

    【讨论】:

    【解决方案3】:

    现在可以使用ExcludeFromCodeCoverage 属性来装饰排除的代码。我一直在使用它并取得了巨大的成功。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多