vignesh ramesh
vignesh ramesh

Reputation: 85

How to Publish Test Results in Azure DevOps

I have created the below YAML file with the PublishTestResults task to display the mocha test results in Azure DevOps portal

# 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'

but whenever I run the build I am getting the following warning

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

The main motive is to display the mocha test results separately in a dashboard or a test tab. So that we don't have to check the test task in build process to see the test results.

Upvotes: 3

Views: 12204

Answers (2)

Jbtf
Jbtf

Reputation: 121

What did help me to publish mocha test results was:

  • First include mocha-junit-reporter in your package.json
npm install mocha-junit-reporter
  • In package.json inside scripts section:
"scripts": {
    "test": "mocha --reporter mocha-junit-reporter"
  }
  • Then publish tests results:
 - task: PublishTestResults@2
   displayName: 'Publish Test Results **/test-results.xml'
   inputs:
     testResultsFiles: '**/test-results.xml'
   condition: succeededOrFailed()

Full azure devops pipeline to run mocha tests and publish results:

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()

Upvotes: 2

Swapna Talloju
Swapna Talloju

Reputation: 86

I encountered same issue, After trying bunch of things could solve it as below.

Step 1. Update 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.

Step 2. In Azure Pipeline YAML file

  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'
    

Publish Test Results

Upvotes: 7

Related Questions