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

t8e9dugd  于 2023-10-20  发布在  其他
关注(0)|答案(2)|浏览(115)

我已经创建了下面的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."

主要目的是在 Jmeter 板或测试选项卡中单独显示mocha测试结果。这样我们就不必在构建过程中检查测试任务来查看测试结果。

uqdfh47h

uqdfh47h1#

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

步骤1.更新package.json

1.包含在scripts部分中:

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

1.在jest config * 或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'

1.发布结果的任务

- 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

xhv8bpkk

xhv8bpkk2#

帮助我发布摩卡测试结果的是:

  • 首先将mocha-junit-reporter包含在您的package.json中
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()

相关问题