Not able to display JUnit tests result in Jenkins Pipeline

青春壹個敷衍的年華 提交于 2019-12-05 08:19:27

FYI if you use declarative pipeline, you can do something like:

pipeline {
   agent any
   stages {
     stage('Build and Test') {
        steps {
            sh 'build here...'
            sh 'run tests here if you like ...'
        }
     }
   }

   post {
      always {
        junit '**/reports/junit/*.xml'
      }
   } 
}

This could also work with html publishing or anything, no need for finally/catch etc. it will always archive the results.

see https://jenkins.io/doc/book/pipeline/syntax/#declarative-pipeline for more

I think the way I was trying to do previous was wrong.I have changed my code like below and it works :

{
        sh("npm install -g gulp bower")
        sh("npm install")
        sh("bower install")
        try {
            sh("gulp test")
        } catch (err) {
            if (currentBuild.result == 'UNSTABLE')
                currentBuild.result = 'FAILURE'
            throw err
        } finally {
            step([$class: 'JUnitResultArchiver', testResults: '**/reports/junit/*.xml', healthScaleFactor: 1.0])
            publishHTML (target: [
                    allowMissing: false,
                    alwaysLinkToLastBuild: false,
                    keepAll: true,
                    reportDir: 'coverage',
                    reportFiles: 'index.html',
                    reportName: "Junit Report"
            ])
        }
    }

Jenkins Pipeline step for publishing JUnit-style test results produced by a Gradle build:

  stage('Publish test results') {
      junit '**/test-results/test/*.xml'
  } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!