Skip to content

Jenkins - Unit Testing

Integrating unit tests is fundamental to CI. Jenkins makes it easy to run unit tests as part of your pipeline and visualize the results.

Jenkins relies on build tools (like Maven, Gradle, Ant, npm, pip) to execute the tests and testing frameworks (like JUnit, TestNG, NUnit, pytest, Jest) to run them and generate report files, typically in XML format.

Common Scenario: Java Unit Tests with Maven/Gradle

Section titled “Common Scenario: Java Unit Tests with Maven/Gradle”

Most Java projects use Maven or Gradle for building and managing dependencies. These tools have built-in support for running JUnit or TestNG tests during the build lifecycle.

  • Maven: The Surefire plugin runs unit tests during the test phase (mvn test or mvn package). It generates XML reports in target/surefire-reports.
  • Gradle: The built-in test task runs tests (usually JUnit by default). It generates XML reports in build/test-results/test and an HTML report in build/reports/tests/test.

The process involves two main steps within your Jenkinsfile:

  1. Execute Tests: Use a build step (sh or bat) to invoke your build tool’s test command.

  2. Publish Results: Use the junit step (provided by the JUnit Plugin, installed by default) to parse the generated XML reports and display them in the Jenkins UI.

pipeline {
agent any // Or a specific agent with Java/Maven configured
stages {
stage('Build & Unit Test') {
steps {
echo 'Compiling and running unit tests...'
// Use 'mvn clean test' or 'mvn clean verify'
// 'verify' runs integration tests too if configured
sh 'mvn clean test'
}
}
// Other stages like Integration Test, Deploy...
}
post {
always {
echo 'Publishing test results...'
// Tell Jenkins where to find JUnit XML reports
junit 'target/surefire-reports/**/*.xml'
}
// You can add specific actions for success/failure here
success {
echo 'Unit tests passed!'
}
failure {
echo 'Unit tests failed!'
// Maybe send a notification
}
}
}
pipeline {
agent any // Or a specific agent with Java/Gradle configured
stages {
stage('Build & Unit Test') {
steps {
echo 'Compiling and running unit tests...'
// Ensure gradlew has execute permissions
sh 'chmod +x ./gradlew'
sh './gradlew build' // 'build' task usually includes 'test'
// Or explicitly: sh './gradlew test'
}
}
}
post {
always {
echo 'Publishing test results...'
// JUnit plugin reads Gradle's XML output too
junit '**/build/test-results/test/*.xml'
// Optional: Publish the HTML report for easy viewing
publishHTML(target: [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: false, // Save space, only keep latest
reportDir: 'build/reports/tests/test',
reportFiles: 'index.html',
reportName: 'Gradle Test Report'
])
}
}
}

After a build runs and the junit step processes the reports:

  • The build status will reflect test failures (Unstable or Failed).
  • A ‘Test Result’ link appears on the build page.
  • Clicking ‘Test Result’ shows a summary: total tests, failures, skipped.
  • You can drill down into specific packages, classes, and failing test methods, often seeing stack traces directly in Jenkins.
  • A ‘Test Result Trend’ graph shows the pass/fail history over recent builds.

The Blue Ocean UI often provides a more modern and visually appealing way to view test results integrated directly within the pipeline view.

The pattern is similar for other ecosystems:

  • JavaScript (Jest/Mocha): Run tests using npm test or yarn test. Configure your test runner to output JUnit XML reports (e.g., using jest-junit). Use the junit step in Jenkins.
  • .NET (NUnit/MSTest): Run tests using dotnet test. Configure it to generate trx or JUnit XML reports. Use the junit step or dedicated plugins if available.
  • Python (pytest/unittest): Run tests using pytest or python -m unittest. Configure pytest to generate JUnit XML (pytest --junitxml=report.xml). Use the junit step.

Always check if your testing framework can produce reports in the JUnit XML format, as this is the most universally understood format by Jenkins.