共用方式為


VSTest@3 - Visual Studio 測試 v3 任務

您可以使用 Visual Studio 測試 (VSTest) 執行器來執行單元和功能測試(Selenium、Appium、自動程式化 UI 測試等)。 您可以執行具有 Visual Studio 測試配接器的測試架構。 範例架構包括 MSTest、xUnit、NUnit、Chutzpah(適用於使用 QUnit、Mocha 和 Jasmine 的 JavaScript 測試),等等。測試可以使用這項工作在多個代理程式上散發。

備註

VSTest@3是工作的最新版本,應該在您的管線中使用。

備註

VSTest@3工作無法重新執行失敗 的資料驅動測試

備註

VSTest Azure 任務特定於 VSTest 平臺。 它不支援較新的 Microsoft.Testing.Platform (MTP)。

語法

# Visual Studio Test v3
# Run unit and functional tests (Selenium, Appium, Coded UI test, etc.) using the Visual Studio Test (VsTest) runner. Test frameworks that have a Visual Studio test adapter such as MsTest, xUnit, NUnit, Chutzpah (for JavaScript tests using QUnit, Mocha and Jasmine), etc. can be run. Tests can be distributed on multiple agents using this task (version 2 and later).
- task: VSTest@3
  inputs:
    #azureSubscription: # string. Alias: ConnectedServiceName. Azure Resource Manager connection. 
  # Test selection
    testSelector: 'testAssemblies' # 'testAssemblies' | 'testPlan' | 'testRun'. Required. Select tests using. Default: testAssemblies.
    testAssemblyVer2: # string. Required when testSelector = testAssemblies. Test files. 
    #testPlan: # string. Required when testSelector = testPlan. Test plan. 
    #testSuite: # string. Required when testSelector = testPlan. Test suite. 
    #testConfiguration: # string. Required when testSelector = testPlan. Test configuration. 
    #tcmTestRun: '$(test.RunId)' # string. Optional. Use when testSelector = testRun. Test Run. Default: $(test.RunId).
    searchFolder: '$(System.DefaultWorkingDirectory)' # string. Required. Search folder. Default: $(System.DefaultWorkingDirectory).
    #resultsFolder: '$(Agent.TempDirectory)\TestResults' # string. Test results folder. Default: $(Agent.TempDirectory)\TestResults.
    #testFiltercriteria: # string. Optional. Use when testSelector = testAssemblies. Test filter criteria. 
    #runOnlyImpactedTests: False # boolean. Optional. Use when testSelector = testAssemblies. Run only impacted tests. Default: False.
    #runAllTestsAfterXBuilds: '50' # string. Optional. Use when testSelector = testAssemblies && runOnlyImpactedTests = true. Number of builds after which all tests should be run. Default: 50.
    #uiTests: false # boolean. Test mix contains UI tests. Default: false.
  # Execution options
    #vstestLocationMethod: 'version' # 'version' | 'location'. Select test platform using. Default: version.
    #vsTestVersion: 'latest' # 'latest' | '17.0' | '16.0' | '15.0' | '14.0' | 'toolsInstaller'. Optional. Use when vstestLocationMethod = version. Test platform version. Default: latest.
    #vstestLocation: # string. Optional. Use when vstestLocationMethod = location. Path to vstest.console.exe. 
    #runSettingsFile: # string. Settings file. 
    #overrideTestrunParameters: # string. Override test run parameters. 
    #pathtoCustomTestAdapters: # string. Path to custom test adapters. 
    #runInParallel: False # boolean. Run tests in parallel on multi-core machines. Default: False.
    #runTestsInIsolation: False # boolean. Run tests in isolation. Default: False.
    #codeCoverageEnabled: False # boolean. Code coverage enabled. Default: False.
    #otherConsoleOptions: # string. Other console options. 
    #diagnosticsEnabled: false # boolean. Collect advanced diagnostics in case of catastrophic failures. Default: false.
    #collectDumpOn: 'onAbortOnly' # 'onAbortOnly' | 'always' | 'never'. Optional. Use when diagnosticsEnabled = true. Collect process dump and attach to test run report. Default: onAbortOnly.
    #rerunFailedTests: False # boolean. Rerun failed tests. Default: False.
    #rerunType: 'basedOnTestFailurePercentage' # 'basedOnTestFailurePercentage' | 'basedOnTestFailureCount'. Optional. Use when rerunFailedTests = true. Do not rerun if test failures exceed specified threshold. Default: basedOnTestFailurePercentage.
    #rerunFailedThreshold: '30' # string. Optional. Use when rerunFailedTests = true && rerunType = basedOnTestFailurePercentage. % failure. Default: 30.
    #rerunFailedTestCasesMaxLimit: '5' # string. Optional. Use when rerunFailedTests = true && rerunType = basedOnTestFailureCount. # of failed tests. Default: 5.
    #rerunMaxAttempts: '3' # string. Optional. Use when rerunFailedTests = true. Maximum # of attempts. Default: 3.
  # Advanced execution options
    #distributionBatchType: 'basedOnTestCases' # 'basedOnTestCases' | 'basedOnExecutionTime' | 'basedOnAssembly'. Batch tests. Default: basedOnTestCases.
    #batchingBasedOnAgentsOption: 'autoBatchSize' # 'autoBatchSize' | 'customBatchSize'. Optional. Use when distributionBatchType = basedOnTestCases. Batch options. Default: autoBatchSize.
    #customBatchSizeValue: '10' # string. Required when distributionBatchType = basedOnTestCases && batchingBasedOnAgentsOption = customBatchSize. Number of tests per batch. Default: 10.
    #batchingBasedOnExecutionTimeOption: 'autoBatchSize' # 'autoBatchSize' | 'customTimeBatchSize'. Optional. Use when distributionBatchType = basedOnExecutionTime. Batch options. Default: autoBatchSize.
    #customRunTimePerBatchValue: '60' # string. Required when distributionBatchType = basedOnExecutionTime && batchingBasedOnExecutionTimeOption = customTimeBatchSize. Running time (sec) per batch. Default: 60.
    #dontDistribute: False # boolean. Replicate tests instead of distributing when multiple agents are used in the job. Default: False.
  # Reporting options
    #testRunTitle: # string. Test run title. 
    #platform: # string. Build platform. 
    #configuration: # string. Build configuration. 
    #publishRunAttachments: true # boolean. Upload test attachments. Default: true.
    #donotPublishTestResults: false # boolean. Optional. Use when runInParallel = false. Disable publishing test results. Default: false.
    #failOnMinTestsNotRun: False # boolean. Fail the task if a minimum number of tests are not run. Default: False.
    #minimumExpectedTests: '1' # string. Optional. Use when failOnMinTestsNotRun = true. Minimum # of tests. Default: 1.

輸入

azureSubscription - Azure Resource Manager 連線
輸入別名ConnectedServiceNamestring

指定以工作負載身分識別同盟設定的 Azure Resource Manager 服務連線,以在整合測試中使用 azurePipelinesCredential。 如需詳細資訊,請參閱 在整合測試中使用 AzurePipelinesCredential

備註

此輸入僅支持設定為使用工作負載身分識別同盟的 ARM 服務連線。


testSelector - 使用 選取測試
string。 必須的。 允許的值:testAssemblies(測試元件)、testPlan(測試計劃)、testRun(測試回合)。 預設值: testAssemblies


testAssemblyVer2 - 測試檔案
stringtestSelector = testAssemblies時為必要項。 預設值: **\bin\**\*test.dll\n**\bin\**\*tests.dll

從指定的檔案執行測試。 藉由分別指定 .orderedtest.webtest 檔案,即可執行已排序的測試與 Webtest。 若要執行 .webtest,需要 Visual Studio 2017 Update 4 或更高版本。 檔案路徑相對於搜尋資料夾。 這個輸入支援多行 迷你模式

# Example
- task: VSTest@3
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.dll
      !**\*TestAdapter.dll
      !**\obj\**

testPlan - 測試計劃
stringtestSelector = testPlan時為必要項。

指定測試計劃,其中包含具有自動化測試案例的測試套件。


testSuite - 測試套件
stringtestSelector = testPlan時為必要項。

指定一或多個包含自動化測試案例的測試套件。 測試案例工作項目必須與 自動化測試方法相關聯。


testConfiguration - 測試組態
stringtestSelector = testPlan時為必要項。

指定測試組態。


tcmTestRun - 測試回合
string。 選擇性。 testSelector = testRun時使用 。 預設值: $(test.RunId)

指定從 測試計畫觸發自動化測試回合時所使用的測試回合型選取專案,。 此選項無法用於在 CI/CD 管線中執行測試。


searchFolder - 搜尋資料夾
string。 必須的。 預設值: $(System.DefaultWorkingDirectory)

指定要搜尋測試元件的資料夾。


resultsFolder - 測試結果資料夾
string。 預設值: $(Agent.TempDirectory)\TestResults

指定要儲存測試結果的資料夾。 使用預設目錄時,它會在管線執行結束時清除。 執行測試之前,一律會在 vstest 工作開始時清除結果目錄。 如果提供,則會將相對資料夾路徑視為相對於 $(Agent.TempDirectory)


testFiltercriteria - 測試篩選準則
string。 選擇性。 testSelector = testAssemblies時使用 。

指定要從測試元件篩選測試的其他準則。 例如: Priority=1|Name=MyTestMethod 。 可用於 - 否定篩選器。 有關篩選條件和選項的更多資訊,請參閱 搜索和篩選測試清單

瞭解 命令行選項。


runOnlyImpactedTests - 只執行受影響的測試
boolean。 選擇性。 testSelector = testAssemblies時使用 。 預設值: False

自動指定並執行驗證程式代碼變更所需的測試。 瞭解如何使用 測試影響分析


runAllTestsAfterXBuilds - 應該執行所有測試的組建數目
string。 選擇性。 testSelector = testAssemblies && runOnlyImpactedTests = true時使用 。 預設值: 50

指定要在自動執行所有測試之前執行的組建數目。 測試影響分析會儲存測試案例與原始碼之間的對應。 建議您定期執行所有測試來重新產生對應。


uiTests - 測試混合包含UI測試
boolean。 預設值: false

若要執行 UI 測試,請確定代理程式設定為以 互動模式執行, 啟用 Autologon。 在將組建/發行排入佇列之前,必須先設定代理程式以互動方式執行。 核取此方塊不會自動在互動式模式中設定代理程式。 此選項可提醒您適當地設定代理程式,以避免失敗。 VS 2015 和 2017 集區的託管 Windows 代理程式可用來執行 UI 測試。


vstestLocationMethod - 使用 選取測試平臺
string。 允許的值:versionlocation (特定位置)。 預設值: version

指定要使用的測試平臺。


vsTestVersion - 測試平臺版本
string。 選擇性。 vstestLocationMethod = version時使用 。 允許的值:latest17.0 (Visual Studio 2022)、16.0 (Visual Studio 2019)、15.0(Visual Studio 2017)、14.0(Visual Studio 2015)、toolsInstaller(由工具安裝程式安裝)。 預設值: latest

指定要使用的 Visual Studio 測試版本。 如果指定 最新的,則此輸入會選擇已安裝的最新版本(從允許的值清單中)。 若要在代理程式上執行不需要 Visual Studio 的測試,請使用 [工具安裝 安裝程式] 選項。 請務必包含 Visual Studio Test Platform Installer 工作,以從 NuGet 取得測試平臺。


vstestLocation - vstest.console.exe 的路徑
string。 選擇性。 vstestLocationMethod = location時使用 。

指定 VSTest 的路徑。


runSettingsFile - 配置檔案
string

指定要與測試搭配使用之 runsettingstestsettings 檔案的路徑。 針對 Visual Studio 15.7 和更新版本,請對所有測試類型使用 runsettings。 深入瞭解 如何將 .testsettings 檔案轉換成 .runsettings 檔案


overrideTestrunParameters - 覆寫測試回合參數
string

覆寫 TestRunParameters 檔案的 runsettings 區段中定義的參數,或 Properties 檔案的 testsettings 區段。 例如: -key1 value1 -key2 value2注意: 使用 Visual Studio 2017 testsettings 存取 TestContext 檔案中指定的屬性(update 4 或更新版本)。


pathtoCustomTestAdapters - 自定義測試配接器的路徑
string

指定自訂測試配接器的目錄路徑。 會自動探索位於與測試元件位於相同資料夾中的配接器。


runInParallel - 在多核心計算機上平行執行測試
boolean。 預設值: False

如果設定為 true,則會以平行方式執行測試,並利用機器的可用核心。 如果在 MaxCpuCount 檔案中指定,這會覆寫 runsettings。 深入瞭解如何平行執行 測試


runTestsInIsolation - 隔離執行測試
boolean。 預設值: False

在隔離進程中執行測試。 這可能會導致 vstest.console.exe 測試程式中的錯誤較少,但測試的執行速度可能會變慢。 使用多重代理程式作業設定執行時,目前無法使用此選項。


已啟用程式代碼涵蓋範圍 codeCoverageEnabled -
boolean。 預設值: False

從測試回合收集程式代碼涵蓋範圍資訊。


otherConsoleOptions - 其他控制台選項
string

可以傳遞至 vstest.console.exe的其他控制台選項

不支援這些選項,而且在使用代理程式作業的 多重代理程式平行 設定執行測試、使用 [測試計劃][測試回合] 選項執行測試時,或選取自定義批處理選項時,將會忽略這些選項。 您可以改用設定檔來指定選項。


distributionBatchType - Batch 測試
string。 允許的值:basedOnTestCases(根據測試和代理程式的數目)、basedOnExecutionTime(根據過去的測試運行時間)、basedOnAssembly(根據測試元件)。 預設值: basedOnTestCases

批次是一組測試。 一批測試會同時執行其測試,並針對批次發佈結果。 如果工作執行所在的作業設定為使用多個代理程式,則每個代理程式會挑選任何可用的測試批次,以平行方式執行。 可以執行批次:

根據測試和代理程式的數目。 根據參與測試回合的測試與代理程式數目,進行簡單的批處理。

根據測試的過去運行時間。 此批處理會考慮過去運行時間,以建立每個批次大約相等運行時間的測試批次。

以測試元件為基礎。 來自元件的測試會分批在一起。


batchingBasedOnAgentsOption - Batch 選項
string。 選擇性。 distributionBatchType = basedOnTestCases時使用 。 允許的值:autoBatchSize(自動判斷批次大小)、customBatchSize(指定批次大小)。 預設值: autoBatchSize

根據參與測試回合的測試與代理程式數目,指定簡單的批處理。 自動判斷批次大小時,每個批次都會包含 (total number of tests / number of agents) 測試。 如果指定批次大小,則每個批次都會包含指定的測試數目。


customBatchSizeValue - 每個批次的測試數目
stringdistributionBatchType = basedOnTestCases && batchingBasedOnAgentsOption = customBatchSize時為必要項。 預設值: 10

指定批次大小。


batchingBasedOnExecutionTimeOption - Batch 選項
string。 選擇性。 distributionBatchType = basedOnExecutionTime時使用 。 允許的值:autoBatchSize(自動判斷批次時間)、customTimeBatchSize(指定每個批次的運行時間)。 預設值: autoBatchSize

此批處理會考慮過去的運行時間,以建立每個批次大約相等運行時間的測試批次。 快速執行的測試將會一起批處理,而長時間執行的測試可能屬於個別的批次。 當此選項與多重代理程式作業設定搭配使用時,總測試時間會縮減為最小值。


每個批次 customRunTimePerBatchValue - 運行時間 (秒)
stringdistributionBatchType = basedOnExecutionTime && batchingBasedOnExecutionTimeOption = customTimeBatchSize時為必要項。 預設值: 60

指定每個批次的運行時間(以秒為單位)。


dontDistribute - 復寫測試,而不是在作業中使用多個代理程式時散發
boolean。 預設值: False

當工作在多代理程式作業中執行時,選擇此選項不會將測試分散到代理程式。 每個選取的測試都會在每個代理程式上重複。 當代理程式作業設定為不使用平行處理原則或多重組態選項執行時,此選項不適用。


testRunTitle - 測試回合標題
string

指定測試回合的名稱。


platform - 建置平臺
string

指定應該報告測試的組建平臺。 如果您已為建置工作中的平臺定義變數,請使用該變數搭配此輸入。


configuration - 建置組態
string

指定應該報告測試的組建組態。 如果您已在建置工作中定義組態的變數,請使用該變數搭配此輸入。


publishRunAttachments - 上傳測試附件
boolean。 預設值: true

加入加入或退出發佈執行層級附件。


donotPublishTestResults - 禁用發佈測試結果
boolean。 選擇性。 runInParallel = false時使用 。 預設值: false

如果啟用此選項,則不會將測試結果作為此任務的一部分發佈。 請改用 Publish Test Results 任務。


failOnMinTestsNotRun - 如果未執行最少的測試數目,工作會失敗。
boolean。 預設值: False

如果未執行最少的測試數目,工作就會失敗。 如果對工作輸入或基礎測試配接器相依性所做的任何變更只會導致找到所需測試的子集,這可能很有用。


minimumExpectedTests - 測試的最小值 #
string。 選擇性。 failOnMinTestsNotRun = true時使用 。 預設值: 1

指定要執行的測試數目下限,讓工作成功。 執行的總測試會計算為通過、失敗和中止的測試總和。


diagnosticsEnabled - 在發生重大失敗時收集進階診斷
boolean。 預設值: false

收集診斷數據以針對重大失敗進行疑難解答,例如測試當機。 核取此選項時,會產生序列 XML 檔案並附加至測試回合。 序列檔案包含測試執行的順序相關信息,因此可以識別潛在的罪魁禍首測試。


collectDumpOn - 收集進程傾印並附加至測試回合報告
string。 選擇性。 diagnosticsEnabled = true時使用 。 允許的值:onAbortOnly(僅限中止)、alwaysnever。 預設值: onAbortOnly

收集可用於進一步分析的迷你傾印。

  • onAbortOnly - 只有在測試回合中止時,才會收集迷你傾印。
  • Always - 無論測試回合是否完成,都會收集迷你傾印。
  • 永不 - 無論測試回合是否完成,都不會收集迷你傾印。

rerunFailedTests - 重新執行失敗的測試
boolean。 預設值: False

重新執行任何失敗的測試,直到通過或達到嘗試次數上限為止。


rerunType - 如果測試失敗超過指定的臨界值,請勿重新執行
string。 選擇性。 rerunFailedTests = true時使用 。 允許的值:basedOnTestFailurePercentage(% 失敗)、basedOnTestFailureCount(失敗的測試# )。 預設值: basedOnTestFailurePercentage

當失敗率超過指定的臨界值時,請避免重新執行測試。 如果環境問題導致大量失敗,則適用此狀況。 您可以將失敗百分比或失敗的測試數目指定為臨界值。


rerunFailedThreshold - % 失敗
string。 選擇性。 rerunFailedTests = true && rerunType = basedOnTestFailurePercentage時使用 。 預設值: 30

當失敗的測試案例百分比超過指定的臨界值時,請避免重新執行測試。 如果環境問題導致大量失敗,則適用此狀況。


rerunFailedTestCasesMaxLimit - # 的失敗測試
string。 選擇性。 rerunFailedTests = true && rerunType = basedOnTestFailureCount時使用 。 預設值: 5

當失敗的測試案例數目超過指定的限制時,請避免重新執行測試。 如果環境問題導致大量失敗,則適用此狀況。


rerunMaxAttempts - 嘗試次數上限 #
string。 選擇性。 rerunFailedTests = true時使用 。 預設值: 3

指定重試失敗測試的次數上限。 如果測試在達到嘗試次數上限之前通過,將不會再次重新執行。


工作控制選項

除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控制項選項和一般工作屬性

輸出變數

沒有。

備註

您可以使用 Visual Studio 測試執行器來執行單元和功能測試(Selenium、Appium、自動程式化 UI 測試等等)。 除了 MSTest 型測試之外,也可以執行具有 Visual Studio 測試配接器的測試架構,例如 xUnit、NUnit 或 Chutzpah。

藉由在 .runsettings 檔案中指定適當的目標 Framework 值 ,以測試目標 .NET core 架構可以執行

測試可以使用這項工作第 2 版在多個代理程式上散發。 如需詳細資訊,請參閱 使用 Visual Studio 測試工作平行執行測試

檢查必要條件

如果您使用 Windows 自我載入代理程式,則必須安裝此必要條件:

需求

代理程式必須具備下列功能:

VS測試

vstest 需求可透過兩種方式滿足:

  1. Visual Studio 已安裝在代理程式電腦上。
  2. 在管線定義中使用 Visual Studio Test Platform Installer 工作

如何執行使用TestCase作為數據源的測試?

若要執行使用 TestCase 作為資料源的自動化測試,需要下列專案:

  1. 您必須在代理程式計算機上擁有 Visual Studio 2017.6 或更高版本。 Visual Studio Test Platform Installer 工作無法用來執行使用 TestCase 作為數據源的測試。
  2. 建立 PAT,此範圍已獲授權「工作專案」(完整)。
  3. 新增名為 Test.TestCaseAccessToken 的安全組建或發行變數,並將值設定為上一個步驟中建立的 PAT。

使用某些任務選項運行數據驅動的 xUnit、NUnit 和 MSTest 測試時,我遇到了問題。 是否有已知的限制?

使用 xUnit、NUnit 和 MSTest 測試架構的資料驅動測試有一些已知限制, 而且無法 與下列工作選項搭配使用:

  1. 重新執行失敗的測試。
  2. 在多個代理程式和批處理選項上散發測試。
  3. 測試影響分析。

上述限制是因為這些測試架構的配接器如何探索和報告數據驅動測試。

VSTest 工作是否支援一次執行以多個目標架構為目標的測試?

是,從版本 17.3 VSTest 開始,支援一次以多個目標架構為目標的測試。 在此之前,由於 VSTest 平臺 端的限制,因此無法達到此狀況。

如果您想要執行屬於多個目標架構的測試,您必須透過visual Studio Test Platform Installer 安裝相容的 VSTest 版本,並將 vsTestVersion 設定為 toolsInstaller 才能使用它。

發佈測試結果時,收到此錯誤:無法發佈測試結果:指定的優先順序無效?

如果任何測試方法的優先順序設定在 255 以上,請修正程式代碼中的測試方法優先順序,然後再次執行測試,就會發生此錯誤。 您可以檢閱產生的 trx 檔案,以查看優先順序大於 255 的所有測試。

在整合測試中使用 AzurePipelinesCredential

適用於 .NET、C++、Go、Java、JavaScript 和 Python 的 Azure 身分識別連結庫 支援工作負載身分識別同盟,因此從 AzureCLI@2AzurePowerShell@5 工作執行的程式代碼可以使用 Microsoft Entra ID 進行驗證,例如,使用 AzurePipelinesCredential 類別存取 Azure。

許多客戶都在從其他工作叫用的整合測試中使用 Azure SDK 用戶端連結庫。 DotNetCoreCLI@2Maven@4VSTest@3 工作可以使用 AzurePipelinesCredential 類別存取 Azure 資源。

您可以將 connectedServiceName 屬性設定為使用工作負載身分識別同盟設定的 Azure 服務連線。 AzurePipelinesCredential 需要設定 SYSTEM_ACCESSTOKEN

下列範例顯示 connectedServiceName 工作上的 DotNetCoreCLI@2 輸入。 VSTest@3Maven@4 使用方式很類似。

- task: DotNetCoreCLI@2
  inputs:
    command: 'run'
    connectedServiceName: <Azure service connection configured with workload identity federation>
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

如需 AzurePipelinesCredential的詳細資訊,請參閱 使用 AzurePipelinesCredential改善 Azure 服務連線中的安全性狀態。

需求

要求 說明
管線類型 YAML、傳統組建、傳統版本
執行於 Agent、DeploymentGroup
要求 自我裝載代理程式必須具有 功能, 符合下列 需求 來執行使用此工作的工作: vstest
能力 此工作不符合作業中後續工作的任何需求。
命令限制 任意
Settable 變數 任意
代理程式版本 2.144.0 或更新
工作類別 測試