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

3

我已创建以下YAML文件,使用PublishTestResults任务在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://learn.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."

主要目的是在仪表板或测试选项卡中单独显示摩卡测试结果,以便我们无需检查构建过程中的测试任务即可查看测试结果。

当你在本地运行这些测试时,你认为它会创建名为TEST-RESULTS.xml的测试结果文件吗? - undefined
是的,它会在我的本地创建一个名为test-results.xml的文件。 - undefined
嗨 @vigneshramesh,PublishTestResults任务的默认搜索路径是$(System.DefaultWorkingDirectory),在下一个任务中,我们可以看到您归档了根文件夹$(System.DefaultWorkingDirectory),请检查node.zip文件并确保其中包含TEST-RESULTS.xml文件。请检查一下,然后在这里友好地分享结果。 - undefined
嗨 @vigneshramesh,只是想问一下,这个问题现在还在阻碍你吗?有关于这个问题的任何更新吗? - undefined
2个回答

6

我遇到了同样的问题, 尝试了很多方法,最终得以解决。

步骤1. 更新 package.json 文件

  1. Include in scripts section:

    "test": "cross-env CI=true react-scripts test --env=jsdom
    --testPathIgnorePatterns=assets --reporters=default --reporters=jest-junit"
    
  2. Include below in jest config or jest section in package.json

    "jest-junit": {
        "suiteNameTemplate": "{filepath}",
        "outputDirectory": ".",
        "outputName": "junit.xml"
    }
    

    Run npm run test locally and see if junit.xml is created.

步骤2. 在 Azure Pipeline YAML 文件中

  1. Include an Npm task with custom command as below

    - task: Npm@1
      displayName: 'Run Tests'
      inputs:
        command: 'custom'
        workingDir: "folder where your package.json exists"
        customCommand: 'test'
    
  2. Task to publish Results

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

发布测试结果


我注意到这个问题是关于mocha的,而接受的回答却谈到了jest。mocha是否会读取jest的设置? - undefined
我有同样的问题,这并没有帮助。 - undefined

2
帮助我发布mocha测试结果的是:
首先在你的package.json中包含mocha-junit-reporter。
npm install mocha-junit-reporter

在 package.json 文件中的 scripts 部分:
"scripts": {
    "test": "mocha --reporter mocha-junit-reporter"
  }

然后发布测试结果:
 - task: PublishTestResults@2
   displayName: 'Publish Test Results **/test-results.xml'
   inputs:
     testResultsFiles: '**/test-results.xml'
   condition: succeededOrFailed()

完整的Azure DevOps管道用于运行Mocha测试并发布结果:
steps:
 - task: NodeTool@0
   inputs:
    versionSpec: '18.x'
   displayName: "Install Node.js"
   
 - script: |
      npm install
      npm test
   workingDirectory: $(Build.SourcesDirectory)/E2E
   displayName: "npm install and Run mocha tests"
 - task: PublishTestResults@2
   displayName: 'Publish Test Results'
   inputs:
    testResultsFiles: '**/test-results.xml'
   condition: succeededOrFailed()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接