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

4

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

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

3个回答

3
您可以在项目中添加runsettings文件。
在该文件中,您可以列出需要从代码覆盖范围中排除的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>

这个页面这个页面应该可以为您提供有关如何添加和配置Test运行设置文件的详细信息。

希望对您有所帮助。


如果您想知道文件在哪里,那么请转到“测试”菜单->“测试设置”,您将在那里看到一个.runsettings文件。如果没有,则可以根据Microsoft文档上的示例创建一个新的文件,网址是:https://learn.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2019 - Horizon
@Manoj_Choudhari 在我的情况下它不起作用 https://stackoverflow.com/questions/62710466/runsettings-xml-file-not-working-with-dotnet-test-coverage-command - Manish Jain

0

1.你需要在测试项目中添加扩展名为.runsettings的Xml文件。一旦你添加了这个runsettings文件,复制下面的代码片段并粘贴到runsettings文件中。

2.在ModulePaths标签内,有一个Exclude标签。在这个标签中,你可以提到需要从代码覆盖中排除的DLL名称或项目名称。

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>


在我的情况下它不起作用 https://stackoverflow.com/questions/62710466/runsettings-xml-file-not-working-with-dotnet-test-coverage-command - Manish Jain
.*\moq.dll$ 这个条目应该是 .*moq.dll$ 或者 .*\\moq.dll$,因为在正则表达式中转义 m 字符是没有意义的。 - hlovdal

-1

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

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


我喜欢没有支持性评论的踩。 - No Refunds No Returns
我喜欢没有支持性评论的负评。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接