共用方式為


PublishTestResults@2 - 發佈測試結果 v2 任務

將測試結果發佈至 Azure Pipelines。

語法

# Publish Test Results v2
# Publish test results to Azure Pipelines.
- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'JUnit' # 'JUnit' | 'NUnit' | 'VSTest' | 'XUnit' | 'CTest'. Alias: testRunner. Required. Test result format. Default: JUnit.
    testResultsFiles: '**/TEST-*.xml' # string. Required. Test results files. Default: **/TEST-*.xml.
    #searchFolder: '$(System.DefaultWorkingDirectory)' # string. Search folder. Default: $(System.DefaultWorkingDirectory).
    #mergeTestResults: false # boolean. Merge test results. Default: false.
    #failTaskOnFailedTests: false # boolean. Fail if there are test failures. Default: false.
    #failTaskOnFailureToPublishResults: false # boolean. Fail if there is failure in publishing test results. Default: false.
    #failTaskOnMissingResultsFile: false # boolean. Fail if no result files are found. Default: false.
    #testRunTitle: # string. Test run title. 
  # Advanced
    #buildPlatform: # string. Alias: platform. Build Platform. 
    #buildConfiguration: # string. Alias: configuration. Build Configuration. 
    #publishRunAttachments: true # boolean. Upload test results files. Default: true.
# Publish Test Results v2
# Publish test results to Azure Pipelines.
- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'JUnit' # 'JUnit' | 'NUnit' | 'VSTest' | 'XUnit' | 'CTest'. Alias: testRunner. Required. Test result format. Default: JUnit.
    testResultsFiles: '**/TEST-*.xml' # string. Required. Test results files. Default: **/TEST-*.xml.
    #searchFolder: '$(System.DefaultWorkingDirectory)' # string. Search folder. Default: $(System.DefaultWorkingDirectory).
    #mergeTestResults: false # boolean. Merge test results. Default: false.
    #failTaskOnFailedTests: false # boolean. Fail if there are test failures. Default: false.
    #testRunTitle: # string. Test run title. 
  # Advanced
    #buildPlatform: # string. Alias: platform. Build Platform. 
    #buildConfiguration: # string. Alias: configuration. Build Configuration. 
    #publishRunAttachments: true # boolean. Upload test results files. Default: true.

輸入

testResultsFormat - 測試結果格式
輸入別名testRunnerstring。 必須的。 允許的值:JUnitNUnitVSTestXUnitCTest。 預設值: JUnit

指定您想要發佈的結果檔案格式。 支援下列格式:CTestJUnitNUnit 2NUnit 3、Visual Studio Test xUnit 2

小提示

VSTest format 是指 TRX 格式。 因此,如果您使用 Microsoft.Testing.Platform (MTP) 生成 TRX,並且不特定於 VSTest,它也可以正常工作。 由於歷史原因,在引入 MTP 之前,該值為 VSTest。


testResultsFiles - 測試結果檔案
string。 必須的。 預設值: **/TEST-*.xml

指定一或多個測試結果檔案。

  • 您可以使用單一資料夾通配符 (*) 和遞歸通配符 (**)。 例如,**/TEST-*.xml 搜尋所有子目錄中名稱開頭為 TEST- 的所有 XML 檔案。 如果使用 VSTest 做為測試結果格式,則檔類型應變更為 .trx,例如 **/TEST-*.trx
  • 您可以指定多個路徑,並以新行分隔。
  • 此外,接受迷你模式。

例如,!TEST[1-3].xml 會排除名為 TEST1.xmlTEST2.xmlTEST3.xml的檔案。


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

選擇性。 指定要搜尋測試結果檔的資料夾。


mergeTestResults - 合併測試結果
boolean。 預設值: false

當這個布林值 true時,工作會針對單一 測試回合報告所有檔案的測試結果,。 如果值為 false,工作會為每個測試結果檔案建立個別的測試回合。 為了優化以獲得更好的性能,如果結果檔超過100個,則結果將始終合併到一次運行中,即使此選項設定為 false

備註

使用合併測試結果設定來合併來自相同測試架構的檔案,以確保正確計算結果對應和持續時間。


failTaskOnFailedTests - 如果存在測試失敗,則失敗
boolean。 預設值: false

選擇性。 當此布林值的值為 true時,如果結果檔中的任何測試被標記為失敗,則任務將失敗。 默認值為 false,它只會發佈結果檔中的結果。


failTaskOnFailureToPublishResults - 如果發佈測試結果失敗,則失敗
boolean。 預設值: false

When true,如果發佈測試結果失敗,則任務失敗。


failTaskOnMissingResultsFile - 如果未找到結果檔,則失敗
boolean。 預設值: false

如果未找到結果檔,則任務失敗。


testRunTitle - 測試回合標題
string

選擇性。 指定將報告結果的測試回合名稱。 您可以使用組建或發行管線中宣告的變數名稱。


buildPlatform - 構建平臺
輸入別名platformstring

選擇性。 指定應該報告測試回合的組建平臺。 例如:x64x86。 如果您在建置工作中為平臺定義變數,請在這裡使用它。


buildConfiguration - 構建配置
輸入別名configurationstring

選擇性。 指定應該報告測試回合的組建組態。 例如:DebugRelease。 如果您在建置工作中定義組態的變數,請在這裡使用它。


publishRunAttachments - 上傳測試結果檔
boolean。 預設值: true

選擇性。 當這個布爾值 true時,工作會將所有測試結果檔案當做附件上傳至測試回合。


工作控制選項

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

輸出變數

沒有。

備註

執行測試時,此任務會將測試結果發佈到 Azure Pipelines 或 TFS,以提供全面的測試報告和分析體驗。 您可以使用您選擇的支援所需結果格式的測試執行程式。 支持的結果格式包括 CTestJUnit (包括 PHPUnit)、 NUnit 2NUnit 3、Visual Studio Test (TRX) 和 xUnit 2

其他內置任務(如 Visual Studio 測試任務Dot NetCore CLI 任務 )會自動將測試結果發佈到管道。 AntMavenGulpGruntXcode 等任務將發佈結果作為任務中的一個選項提供,或者構建 CoberturaJaCoCo 等庫。 如果您正在使用這些任務中的任何一個,則不需要在管道中使用單獨的 Publish Test Results 任務。

發佈的測試結果顯示在管道摘要的 Tests (測試) 選項卡中 。 結果可説明您衡量管道品質、審查可追溯性、排除故障並推動故障擁有權。

以下示例顯示任務配置為發佈測試結果。

打開測試歷史記錄頁面

還可以在生成管道中使用此任務,將運行測試時生成的 代碼覆蓋率結果發佈到 Azure Pipelines 或 TFS,以獲取覆蓋率報告。

先決條件

如果使用的是 Windows 自承載代理,則電腦必須安裝此先決條件:

任務預設值

默認選項使用 JUnit 格式發佈測試結果。 使用 VSTest 作為 testRunner 時,應將 testResultsFiles 選項更改為 **/TEST-*.trx.

testResultsFormattestRunner 輸入名稱的別名。 結果檔可以由多個 runner 生成,而不僅僅是一個特定的 runner。 例如,許多運行程式都支援 jUnit 結果格式,而不僅僅是 jUnit。

要使用 YAML 發佈 Python 的測試結果,請參閱這些主題的生態系統部分中的 Python,其中還包括其他語言的示例。

結果格式映射

下表列出了內部版本或發佈摘要的 Tests 選項卡中 報告的欄位,以及與支援的測試結果格式中的屬性的相應映射。

影響範圍 領域 Visual Studio 測試 (TRX)
試運轉 標題 任務中指定的測試運行標題
開始日期 /TestRun/Times.Attributes[“start”].價值
完成日期 /TestRun/Times.Attributes[“finish”] 的價值
持續時間 完成日期 - 開始日期
附件 請參閱下面的 附件支援 部分
測試結果 標題 /testRun/Results/UnitTestResult.Attributes[“testName”] 的值或 /testRun/Results/WebTestResult.Attributes[“testName”]。值或 /testRun/Results/TestResultAggregation.Attributes[“testName”]。價值
開始日期 /TestRun/Results/UnitTestResult.Attributes[“startTime”] 的值或 /TestRun/Results/WebTestResult.Attributes[“startTime”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“startTime”]。價值
完成日期 /TestRun/Results/UnitTestResult.Attributes[“startTime”] 的值 + /TestRun/Results/UnitTestResult.Attributes[“duration”]。值或 /TestRun/Results/WebTestResult.Attributes[“startTime”]。值 + /TestRun/Results/WebTestResult.Attributes[“duration”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“startTime”]。值 + /TestRun/Results/TestResultAggregation.Attributes[“duration”]。價值
持續時間 /TestRun/Results/UnitTestResult.Attributes[“duration”] 的Value 或 /TestRun/Results/WebTestResult.Attributes[“duration”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“duration”]。價值
所有者 /TestRun/TestDefinitions/UnitTest/Owners/Owner.Attributes[“name”].價值
結果 /TestRun/Results/UnitTestResult.Attributes[“outcome”] 的值或 /TestRun/Results/WebTestResult.Attributes[“outcome”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“outcome”]。價值
錯誤訊息 /TestRun/Results/UnitTestResult/Output/ErrorInfo/Message.InnerText 或 /TestRun/Results/WebTestResultOutput/ErrorInfo/Message.InnerText 或 /TestRun/Results/TestResultAggregation/Output/ErrorInfo/Message.InnerText
堆疊追蹤 /TestRun/Results/UnitTestResult/Output/ErrorInfo/StackTrace.InnerText 或 /TestRun/Results/WebTestResultOutput/ErrorInfo/StackTrace.InnerText 或 /TestRun/Results/TestResultAggregation/Output/ErrorInfo/StackTrace.InnerText
附件 請參閱下面的 附件支援 部分
主控台紀錄 /TestRun/Results/UnitTestResult/Output/StdOut.InnerText 或 /TestRun/Results/WebTestResultOutput/Output/StdOut.InnerText 或 /TestRun/Results/TestResultAggregation/Output/StdOut.InnerText
主控台錯誤紀錄 /TestRun/Results/UnitTestResult/Output/StdErr.InnerText 或 /TestRun/Results/WebTestResultOutput/Output/StdErr.InnerText 或 /TestRun/Results/TestResultAggregation/Output/StdErr.InnerText
代理程式名稱 /TestRun/Results/UnitTestResult.Attributes[“computerName”] 的值或 /TestRun/Results/WebTestResult.Attributes[“computerName”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“computerName”]。價值
測試檔 /TestRun/TestDefinitions/UnitTest.Attributes[“storage”] 的價值
優先順序 /TestRun/TestDefinitions/UnitTest.Attributes[“priority”] 的價值

備註

僅當Date started (開始日期)Date completed (完成日期 ) 不可用時,才使用 Duration(持續時間)。

testName 的完全限定名稱格式為 Namespace.Testclass.Methodname,字元限制為 512。 如果測試是數據驅動的並且具有參數,則字元限制將包括參數。

發佈測試結果時,您可能會收到以下錯誤:無法發佈測試結果:指定的優先順序無效

如果任何測試方法的優先順序設置為高於 255,請修復代碼中的測試方法優先順序,然後再次執行測試,則會出現此錯誤。 您可以檢閱產生的 trx 檔案,以查看優先順序大於 255 的所有測試。

附件支援

Publish Test Results (發佈測試結果) 任務支援以下格式的測試運行和測試結果的附件。 對於公共項目,我們支援 2GB 的總附件數。

Visual Studio 測試 (TRX)

影響範圍 類型 路徑
測試回合 數據收集器 /TestRun/ResultSummary/CollectorDataEntries/Collector/UriAttachments/UriAttachment/A.Attributes[“href”].價值
測試結果 /TestRun/ResultSummary/ResultFiles/ResultFile.Attributes[“路徑”]。價值
程式碼涵蓋範圍 /TestRun/TestSettings/Execution/AgentRule/DataCollectors/DataCollector/Configuration/CodeCoverage/Regular/CodeCoverageItem.Attributes[“binaryFile”].值和 /TestRun/TestSettings/Execution/AgentRule/DataCollectors/DataCollector/Configuration/CodeCoverage/Regular/CodeCoverageItem.Attributes[“pdbFile”]。價值
測試結果 資料收集器 /TestRun/Results/UnitTestResult/CollectorDataEntries/Collector/UriAttachments/UriAttachment/A.Attributes[“href”].值或 /TestRun/Results/WebTestResult/CollectorDataEntries/Collector/UriAttachments/UriAttachment/A.Attributes[“href”]。值或 /TestRun/Results/TestResultAggregation/CollectorDataEntries/Collector/UriAttachments/UriAttachment/A.Attributes[“href”]。價值
測試結果 /TestRun/Results/UnitTestResult/ResultFiles/ResultFile.Attributes[“路徑”]。值或 /TestRun/Results/WebTestResult/ResultFiles/ResultFile.Attributes[“path”]。值或 /TestRun/Results/TestResultAggregation/ResultFiles/ResultFile.Attributes[“path”]。價值

備註

將測試結果檔作為附件上傳的選項是任務中的預設選項,適用於所有格式。

範例

Docker(用於開發、傳遞和運行應用程式的平台)

對於基於 Docker 的應用程式,有多種方法可以構建應用程式和運行測試:

  • 在生成管道中生成和測試:生成和測試在管道中執行,並使用 Publish Test Results 任務發佈測試結果。
  • 使用多階段 Dockerfile 進行構建和測試:使用多階段 Docker 檔在容器內執行構建和測試,因為此類測試結果不會發佈回管道。
  • 使用 Dockerfile 生成、測試和發佈結果:生成和測試在容器內執行,結果將發佈回管道。 請參閱以下範例。

使用 Docker 檔構建、測試和發佈結果

在這種方法中,您可以使用 Docker 檔在容器內構建代碼並運行測試。 然後,將測試結果複製到主機以發佈到管道。 若要將測試結果發佈到 Azure Pipelines,可以使用 「發佈測試結果 」 任務。 最終映像將發佈到 Docker 或 Azure 容器註冊表。

取得程式碼
  1. 在項目目錄的根目錄中建立一個 Dockerfile.build 檔,其中包含以下內容:

    # Build and run tests inside the docker container
    FROM mcr.microsoft.com/dotnet/sdk:2.1
    WORKDIR /app
    # copy the contents of agent working directory on host to workdir in container
    COPY . ./
    # dotnet commands to build, test, and publish
    RUN dotnet restore
    RUN dotnet build -c Release
    RUN dotnet test dotnetcore-tests/dotnetcore-tests.csproj -c Release --logger "trx;LogFileName=testresults.trx"
    RUN dotnet publish -c Release -o out
    ENTRYPOINT dotnet dotnetcore-sample/out/dotnetcore-sample.dll
    

    此檔包含構建代碼和運行測試的說明。 然後,將測試複製到容器內的檔中 testresults.trx

  2. 要使最終映射盡可能小,僅包含運行時和部署構件,請將 existing Dockerfile 的內容替換為以下內容:

    # This Dockerfile creates the final image to be published to Docker or
    # Azure Container Registry
    # Create a container with the compiled asp.net core app
    FROM mcr.microsoft.com/dotnet/aspnet:2.1
    # Create app directory
    WORKDIR /app
    # Copy only the deployment artifacts
    COPY /out .
    ENTRYPOINT ["dotnet", "dotnetcore-sample.dll"]
    
定義構建管道
  1. 如果您有 Docker Hub 帳戶,並且想要將映射推送到 Docker 註冊表,請將文件內容 .vsts-ci.docker.yml 替換為以下內容:

    # Build Docker image for this app, to be published to Docker Registry
    pool:
      vmImage: 'ubuntu-latest'
    variables:
      buildConfiguration: 'Release'
    steps:
    - script: |
        docker build -f Dockerfile.build -t $(dockerId)/dotnetcore-build:$BUILD_BUILDID .
        docker run --name dotnetcoreapp --rm -d $(dockerId)/dotnetcore-build:$BUILD_BUILDID
        docker cp dotnetcoreapp:app/dotnetcore-tests/TestResults $(System.DefaultWorkingDirectory)
        docker cp dotnetcoreapp:app/dotnetcore-sample/out $(System.DefaultWorkingDirectory)
        docker stop dotnetcoreapp
    
    - task: PublishTestResults@2
      inputs:
        testRunner: VSTest
        testResultsFiles: '**/*.trx'
        failTaskOnFailedTests: true
    
    - script: |
        docker build -f Dockerfile -t $(dockerId)/dotnetcore-sample:$BUILD_BUILDID .
        docker login -u $(dockerId) -p $pswd
        docker push $(dockerId)/dotnetcore-sample:$BUILD_BUILDID
      env:
        pswd: $(dockerPassword)
    

    或者,如果配置 Azure 容器註冊表並希望將映像推送到該註冊表,請將文件的內容 .vsts-ci.yml 替換為以下內容:

    # Build Docker image for this app to be published to Azure Container Registry
    pool:
      vmImage: 'ubuntu-latest'
    variables:
      buildConfiguration: 'Release'
    
    steps:
    - script: |
        docker build -f Dockerfile.build -t $(dockerId)/dotnetcore-build:$BUILD_BUILDID .
        docker run --name dotnetcoreapp --rm -d $(dockerId)/dotnetcore-build:$BUILD_BUILDID
        docker cp dotnetcoreapp:app/dotnetcore-tests/TestResults $(System.DefaultWorkingDirectory)
        docker cp dotnetcoreapp:app/dotnetcore-sample/out $(System.DefaultWorkingDirectory)
        docker stop dotnetcoreapp
    
    - task: PublishTestResults@2
      inputs:
        testRunner: VSTest
        testResultsFiles: '**/*.trx'
        failTaskOnFailedTests: true
    
    - script: |
        docker build -f Dockerfile -t $(dockerId).azurecr.io/dotnetcore-sample:$BUILD_BUILDID .
        docker login -u $(dockerId) -p $pswd $(dockerid).azurecr.io
        docker push $(dockerId).azurecr.io/dotnetcore-sample:$BUILD_BUILDID 
      env:
        pswd: $(dockerPassword)
    
  2. 將更改推送到存儲庫中的main分支。

  3. 如果使用 Azure 容器註冊表,請確保已在 Azure 門戶中 預先創建註冊表 。 複製 Azure 門戶中註冊表設置的 「訪問金鑰 」部分中顯示的管理員使用者名和密碼。

  4. 使用以下命令更新您的構建管道

    • 代理池Hosted Ubuntu 1604
      • dockerId:將值設置為 DockerHub 的 Docker ID 或 Azure 容器註冊表的管理員使用者名。
      • dockerPassword:將值設置為 DockerHub 的密碼或管理員密碼 Azure 容器註冊表。
    • YAML 檔案路徑/.vsts-ci.docker.yml
  5. 將新生成排隊並觀察它創建並將 Docker 映像推送到註冊表,並將測試結果推送到 Azure DevOps。

需求

要求 說明
管線類型 YAML、傳統組建、傳統版本
執行於 Agent、DeploymentGroup
要求 沒有
能力 此工作不符合作業中後續工作的任何需求。
命令限制 任意
Settable 變數 任意
代理程式版本 2.0.0 或更新
工作類別 測試