如何在 Azure DevOps 中发布测试结果

How to Publish Test Results in Azure DevOps

我使用 PublishTestResults 任务创建了以下 YAML 文件,以在 Azure DevOps 门户中显示 mocha 测试结果

# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '10.14.1'
  displayName: 'Install Node.js'

- script: |
    npm install
    mocha test --reporter mocha-junit-reporter
    npm test
  displayName: 'npm install and test'

- task: PublishTestResults@2
  condition: succeededOrFailed()
  inputs:
    testRunner: JUnit
    testResultsFiles: '**/TEST-RESULTS.xml'

- task: ArchiveFiles@2
  displayName: 'Archive $(System.DefaultWorkingDirectory)'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
    includeRootFolder: false
    archiveFile: '$(Build.ArtifactStagingDirectory)/node.zip'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'

但每当我 运行 构建时,我都会收到以下警告

"No test result files matching **/TEST-RESULTS.xml were found."

主要动机是在仪表板或测试选项卡中单独显示 mocha 测试结果。这样我们就不用在构建过程中检查测试任务就可以看到测试结果了。

我遇到了同样的问题, 在尝试了一堆东西之后可以解决它,如下所示。

步骤 1. 更新 package.json

  1. 包含在 scripts 部分:

    "test": "cross-env CI=true react-scripts test --env=jsdom
    --testPathIgnorePatterns=assets --reporters=default --reporters=jest-junit"
    
  2. package.json

    中的 jest 部分中包含下面的笑话配置
    "jest-junit": {
        "suiteNameTemplate": "{filepath}",
        "outputDirectory": ".",
        "outputName": "junit.xml"
    }
    

    运行 npm run test 在本地查看是否创建了 junit.xml

步骤 2. 在 Azure Pipeline YAML 文件中

  1. 包含一个 Npm 带有自定义命令的任务,如下所示

    - task: Npm@1
      displayName: 'Run Tests'
      inputs:
        command: 'custom'
        workingDir: "folder where your package.json exists"
        customCommand: 'test'
    
  2. 发布结果的任务

    - task: PublishTestResults@2
      displayName: 'Publish Unit Test Results'
      condition: succeededOrFailed()
      inputs:
          testResultsFormat: 'JUnit'
          testResultsFiles: '**/junit.xml'
          mergeTestResults: true
          failTaskOnFailedTests: true
          testRunTitle: 'React App Test'
    

Publish Test Results