代码之家  ›  专栏  ›  技术社区  ›  Leonardo

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

  •  0
  • Leonardo  · 技术社区  · 7 年前

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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Manoj Choudhari    7 年前

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

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

    希望这对你有帮助。

        2
  •  0
  •   Ravi Bhushan Kumar    7 年前

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

    2.内部 模块路径 标签,有 排除 标签输入此标记可以提到需要从代码覆盖范围中排除的DLL名称或项目名称。

    3.对于测试项目,我们应该提到项目名称,而不是它的DLL。例如:我的测试项目名是 Skyve.Helper.Document文件.测试 . 所以我在 排除 标签。

    <?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
  •  0
  •   No Refunds No Returns    6 年前

    现在有了 ExcludeFromCodeCoverage 用于修饰排除的代码的属性。我一直用它很成功。

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