Not able to display JUnit tests result in Jenkins Pipeline

痞子三分冷 提交于 2019-12-07 02:17:24

问题


I have a piece of Jenkins pipeline code in which I am trying to run JUnit on my angular code.

If the unit tests fail, Jenkins has to stop the pipeline. It's working except I am not able to see "Latest test Result" and "Test Result Trend"

I am using Jenkins 2.19.1, Jenkins Pipeline 2.4 and Junit 1.19. Here is the pipeline code:

{
        sh("npm install -g gulp bower")
        sh("npm install")
        sh("bower install")    
        try {
            sh("gulp test")
        } catch (err) {
            step([$class: 'JUnitResultArchiver', testResults: '**/reports/junit/*.xml', healthScaleFactor: 1.0])
            junit '**/reports/junit/*.xml'
            if (currentBuild.result == 'UNSTABLE')
                currentBuild.result = 'FAILURE'
            throw err
        }
    }

Any idea what I am doing wrong?


回答1:


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




回答2:


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"
            ])
        }
    }



回答3:


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

  stage('Publish test results') {
      junit '**/test-results/test/*.xml'
  } 


来源:https://stackoverflow.com/questions/41230668/not-able-to-display-junit-tests-result-in-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!