Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to display JUnit tests result in Jenkins Pipeline

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?

like image 671
Joy Avatar asked Dec 19 '16 20:12

Joy


1 Answers

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.

If you have a clean target that results in no test output:

  post {
    always {
      junit(
        allowEmptyResults: true,
        testResults: '**/test-reports/*.xml'
      )
    }
like image 83
Michael Neale Avatar answered Nov 09 '22 15:11

Michael Neale