自定义代码覆盖率分析

默认情况下,Visual Studio 2012 代码复盖率工具分析加载在单元测试中的所有解决方案程序集 (.exe/.dll)。 建议将此默认值,因为它在大多数情况下可以很好地工作。 有关更多信息,请参见使用代码覆盖率确定所测试的代码量

在自定义代码复盖率行为之前,请考虑一些选择:

  • 我希望从代码复盖率结果中排除测试代码以及仅包含应用程序代码。

    添加 ExcludeFromCodeCoverage Attribute 到测试选件类。

  • 我希望包括不是"我的解决方案的程序集。

    获取这些程序集的 .pdb 文件并将它们复制到文件夹和程序集 .dll 文件具有相同。

使用文件扩展名 .runsettings,若要自定义代码复盖率行为,请复制 本主题末尾的示例 并将其添加到您的解决方案。 编辑到您的需要,然后在 测试 菜单上,选择 测试设置选择测试设置 文件。 本主题其余部分将更详细地介绍此过程。

.runsettings 文件

高级代码复盖率设置在 .runsettings 文件中指定。 这是单元使用的配置文件测试工具。 建议您复制 本主题末尾的示例 和编辑以符合您的需要。

  • 发生在"我在使用 Visual Studio 2010 中的名为 .testsettings 文件?

    在 Visual Studio 2010 中,名为 .testsettings 文件仅适用于单元测试基于 MSTest 结构。 在 Visual Studio 2012 中,测试工具不仅适用于 MSTest,而且其他框架 (如 NUnit 和 xUnit。 net”。 .testsettings 文件不会使用这些一起使用。 .runsettings 文件中设计自定义测试工具用与所有测试框架使用的方法。

若要自定义代码复盖率,则需要添加 .runsettings 文件添加到解决方案中:

  1. 添加一个 .xml 文件为一个扩展 .runsettings的解决方案项:

    在解决方案资源管理器中,解决方案快捷菜单上,选择 添加新建项,然后选择 XML 文件。 保存具有名称结尾的文件 (如 CodeCoverage.runsettings

  2. 添加到该示例提供的内容本主题末尾,然后自定义到您的需要如下一节所述。

  3. 测试 菜单中,选择 测试设置选择测试设置文件 并选择一个文件。

  4. 现在,当您运行 测试分析代码覆盖率时,此 .runsettings 文件将控制其行为。

  5. 和关闭若要启用自定义设置,请取消选择或选择" 测试的文件,测试设置 菜单。

包含自定义设置文件的测试设置菜单

部件的其他方面在同一 .runsettings 文件测试可以配置。 有关更多信息,请参见使用单元测试验证代码

JJ159530.collapse_all(zh-cn,VS.110).gif指定符号搜索路径

代码复盖率需要符号 (.pdb 文件) 该程序集可以存在。 对于您的解决方案生成的程序集,符号文件及二进制文件通常存在,并且,代码复盖率自动工作。 但是,在某些情况下,可以在代码复盖率分析可能希望包括引用的程序集。 在这种情况下,.pdb 文件可能不是用双引号括,但是,在 .runsettings 文件中指定符号搜索路径。

         <SymbolSearchPaths>              
               <Path>\\mybuildshare\builds\ProjectX</Path>
               <!--More paths if required-->
         </SymbolSearchPaths>

警告

符号解析可能需要很长时间,特别是,在使用具有大量程序集的某远端文件位置。因此,请考虑远程 .pdb 文件复制到本地位置和二进制 (.dll 和 .exe) 文件同名。

JJ159530.collapse_all(zh-cn,VS.110).gif排除而且包含

可以排除指定了从代码复盖率分析的程序集。 例如:

<ModulePaths>
  <Exclude>
   <ModulePath>Fabrikam.Math.UnitTest.dll</ModulePath>
   <!-- Add more ModulePath nodes here. -->
  </Exclude>
</ModulePaths>

或者,可以指定哪些程序集应包含的。 此方法具有的缺点,当添加多个程序集添加到解决方案时,必须确保将它们添加到列表:

<ModulePaths>
  <Include>
   <ModulePath>Fabrikam.Math.dll</ModulePath>
   <!-- Add more ModulePath nodes here. -->
  </Include>
</ModulePaths>

如果 <Include> 为空,则代码复盖率过程包括所有程序集 (.dll 和 .exe 文件) 加载,并为其 .pdb 文件中找到,但与 <Exclude> 的子句列表中选择项。

Include 在 Exclude之前处理。

JJ159530.collapse_all(zh-cn,VS.110).gif正则表达式

包括排除节点使用正则表达式。 有关更多信息,请参见在 Visual Studio 中使用正则表达式。 正则表达式与通配符。 具体而言:

  1. . * 匹配任意字符字符串

  2. \。匹配一个点“. ")

  3. \( \) (\) 匹配括号“()”

  4. \\ 匹配的文件路径分隔符“\”

  5. ^ 与该字符串的开头

  6. $ 与该字符串的结尾

所有匹配项不区分大小写。

例如:

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

警告

如果在正则表达式中的错误,例如非转义和不对圆括号,则代码复盖率分析不会运行。

JJ159530.collapse_all(zh-cn,VS.110).gif其他方式包括或排除元素

有关示例 本主题末尾的示例 参见。

  • ModulePath –集文件路径指定的程序集。

  • CompanyName –由提供属性的与程序集。

  • PublicKeyToken –匹配由公钥标记签名的程序集。 例如与任何 Visual Studio 组件和扩展,请使用 <PublicKeyToken>^B03F5F7F11D50A3A$</PublicKeyToken>。

  • Source –在定义它们源文件的路径名称匹配元素。

  • Attribute –特殊特性附加匹配元素。 指定特性的全名,包括“属性”在名称的末尾。

  • Function –匹配程序、函数或方法由完全限定名。

与函数名

您的正则表达式必须与函数的完全限定名,包括命名空间,类名,方法名称,并且参数列表。 例如,

  • C# 或 Visual Basic: Fabrikam.Math.LocalMath.SquareRoot(double)

  • C++: Fabrikam::Math::LocalMath::SquareRoot(double)

<Functions>
  <Include>
    <!-- Include methods in the Fabrikam namespace: -->
    <Function>^Fabrikam\..*</Function>
    <!-- Include all methods named EqualTo: -->
    <Function>.*.\EqualTo\(.*</Function>
  </Include>
  <Exclude>
    <!-- Exclude methods in a class or namespace named UnitTest: -->
    <Function>.*\.UnitTest\..*</Function>
  </Exclude>
</Functions>

如何指定 .runsettings 文件,以便在运行测试时

JJ159530.collapse_all(zh-cn,VS.110).gif若要自定义在 Visual Studio 中 runsettings 测试

选择 测试测试设置选择测试设置文件 并选择 .runsettings 文件。 文件会在测试设置菜单,因此,您可以选择或取消。 当选择时,您的 .runsettings 文件应用,只要您使用 分析代码覆盖率

JJ159530.collapse_all(zh-cn,VS.110).gif若要自定义在命令行上运行设置测试

若要从命令行运行测试,请使用 vstest.console.exe。 设置文件是此实用工具的参数。 有关更多信息,请参见从命令行使用 VSTest.Console

  1. 生成 Visual Studio 开发人员命令提示:

    在窗口 启动上,选择 所有程序Microsoft Visual Studio中,Visual Studio 工具开发人员命令提示

  2. 运行:

    vstest.console.exe MyTestAssembly.dll /EnableCodeCoverage /Settings:CodeCoverage.runsettings

JJ159530.collapse_all(zh-cn,VS.110).gif若要自定义请运行在生成定义的设置

您可以获取代码从团队生成的复盖率数据。

在生成定义中指定 runsetting

  1. 确定您的 .runsettings 文件签入。

  2. 在“团队资源管理器”中,打开**“生成”**,然后添加或编辑生成定义。

  3. 过程 页上,展开 自动测试测试源运行设置。 选择您的 .runsettings 文件。

    • 但是,测试程序集 显示而不是 测试源。 当我尝试设置 运行设置 字段时,我只能选择名为 .testsettings 文件。

      自动测试,选择下的 测试程序集,并选择 [...] 在行尾。 在 添加/编辑测试运行 对话框中,设置 测试运行程序Visual Studio 测试运行程序

结果是显示在生成报表的"摘要"部分。

示例 .runsettings 文件

将此代码复制并编辑以符合您的需要。 这是默认值 .runsettings 文件。

<?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=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
        <Configuration>
          <CodeCoverage>
<!--
Additional paths to search for .pdb (symbol) files. Symbols must be found for modules to be instrumented.
If .pdb files are in the same folder as the .dll or .exe files, they are automatically found. Otherwise, specify them here.
Note that searching for symbols increases code coverage runtime. So keep this small and local.
--> 
<!--           
            <SymbolSearchPaths>              
                   <Path>C:\Users\User\Documents\Visual Studio 2012\Projects\ProjectX\bin\Debug</Path>
                   <Path>\\mybuildshare\builds\ProjectX</Path>
            </SymbolSearchPaths>
-->

<!--
About include/exclude lists:
Empty "Include" clauses imply all; empty "Exclude" clauses imply none.
Each element in the list is a regular expression (ECMAScript syntax). See https://msdn.microsoft.com/library/2k3te2cs.aspx.
An item must first match at least one entry in the include list to be included.
Included items must then not match any entries in the exclude list to remain included.
-->

            <!-- Match assembly file paths: -->
            <ModulePaths>
              <Include>
                <ModulePath>.*\.dll$</ModulePath>
                <ModulePath>.*\.exe$</ModulePath>
              </Include>
              <Exclude>
                <ModulePath>.*CPPUnitTestFramework.*</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>
              </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>
              </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>
              </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>
                <PublicKeyToken>^B77A5C561934E089$</PublicKeyToken>
                <PublicKeyToken>^B03F5F7F11D50A3A$</PublicKeyToken>
                <PublicKeyToken>^31BF3856AD364E35$</PublicKeyToken>
                <PublicKeyToken>^89845DCD8080CC91$</PublicKeyToken>
                <PublicKeyToken>^71E9BCE111E9429C$</PublicKeyToken>
                <PublicKeyToken>^8F50407C4E9E73B6$</PublicKeyToken>
                <PublicKeyToken>^E361AF139669C375$</PublicKeyToken>
              </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>
  </DataCollectionRunSettings>
</RunSettings>

请参见

概念

使用单元测试验证代码

其他资源

使用代码覆盖率确定所测试的代码量