Test Reports

Publish test results with junit so Jenkins tracks pass/fail trends and highlights new failures.

Syntaxjunit 'target/surefire-reports/*.xml'

Running tests is not enough — you want Jenkins to record the results. The junit step reads test report files (in JUnit XML format) and displays them.

What you get

  • A test result summary on each build page.
  • Trend graphs over time.
  • Detailed views of which tests failed and why.
  • Builds marked unstable (yellow) when tests fail.

Report formats

Most test frameworks can output JUnit XML: JUnit and TestNG (Java), pytest (--junitxml), Jest (jest-junit), and many more.

Example

Example · bash
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'pytest --junitxml=reports/results.xml'
            }
        }
    }
    post {
        always {
            junit 'reports/*.xml'
        }
    }
}

When to use it

  • A Java project publishes Maven Surefire XML reports after each build so Jenkins tracks test pass/fail trends over time.
  • A JavaScript project writes Jest output to JUnit XML format and publishes it so failing tests are highlighted in the UI.
  • A team uses the trend graph on the job page to identify tests that started failing after a specific commit was merged.

More examples

Publish JUnit XML from Maven

Runs Maven tests and publishes the Surefire XML reports using the JUnit plugin after every run.

Example · groovy
stage('Test') {
  steps { sh 'mvn test' }
  post {
    always {
      junit 'target/surefire-reports/*.xml'
    }
  }
}

Jest with JUnit reporter

Runs Jest with the jest-junit reporter to produce JUnit XML output, then publishes it to Jenkins.

Example · groovy
stage('Test') {
  steps {
    sh 'npm test -- --reporters=jest-junit'
  }
  post {
    always {
      junit 'junit.xml'
    }
  }
}

JUnit with test result threshold

Publishes test results and escalates the build from UNSTABLE to FAILED when test failures are detected.

Example · groovy
post {
  always {
    junit(
      testResults: 'reports/**/*.xml',
      skipMarkingBuildUnstable: false,
      allowEmptyResults: false
    )
  }
  unstable {
    error('Test failures detected — failing the build')
  }
}

Discussion

  • Be the first to comment on this lesson.