自訂程式碼涵蓋範圍分析

根據預設, 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-tw,VS.110).gif指定符號搜尋路徑。

程式碼涵蓋範圍要求符號 (.pdb 檔案) 表示組件可以存在。 對於您的方案中建立的組件,其符號檔沿著二進位檔案呈現,通常是以及程式碼涵蓋範圍自動運作。 但是在某些案例中,您可以在程式碼涵蓋範圍分析可能要加入參考的組件。 在這種情況下, .pdb 檔案可能不是二進位的,不過您可以在 .runsettings 檔案中指定符號搜尋路徑。

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

警告

符號解析可能需要一些時間,特別是在使用具有大量組件時的遠端檔案位置。因此,請考慮複製遠端 .pdb 檔案複製至本機位置,已和二進位 (.dll 和 .exe) 檔相同。

JJ159530.collapse_all(zh-tw,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-tw,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-tw,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-tw,VS.110).gif自訂 Visual Studio 測試的執行設定

選取 [測試], [測試設定], [選取測試設定檔] 並選取 .runsettings 檔案。 檔案隨即出現在測試設定中使用功能表,您可以選取或移除它。 當選取時,您的 .runsettings 檔套用 [分析程式碼涵蓋範圍]。

JJ159530.collapse_all(zh-tw,VS.110).gif自訂在命令列測試的回合設定

若要從命令列執行測試,請使用 vstest.console.exe。 設定檔是公用程式參數。 如需詳細資訊,請參閱從命令列使用 VSTest.Console

  1. 啟動 Visual Studio Developer 命令提示字元中輸入:

    在視窗中 [開始] 上,選取 [所有程式], [Microsoft Visual Studio], [Visual Studio 工具], [開發人員命令提示字元]。

  2. 執行:

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

JJ159530.collapse_all(zh-tw,VS.110).gif自訂在組建定義的回合設定

您可以從 Team Build 取得程式碼涵蓋範圍資料。

在組建定義中指定回合設定

  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>

請參閱

概念

使用單元測試驗證程式碼

其他資源

使用程式碼涵蓋範圍來決定所測試的程式碼數量