Jenkins - Unit Testing
Jenkins - Unit Testing Integration
Section titled “Jenkins - Unit Testing Integration”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
testphase (mvn testormvn package). It generates XML reports intarget/surefire-reports. - Gradle: The built-in
testtask runs tests (usually JUnit by default). It generates XML reports inbuild/test-results/testand an HTML report inbuild/reports/tests/test.
Integrating into Jenkins Pipeline
Section titled “Integrating into Jenkins Pipeline”The process involves two main steps within your Jenkinsfile:
-
Execute Tests: Use a build step (
shorbat) to invoke your build tool’s test command. -
Publish Results: Use the
junitstep (provided by the JUnit Plugin, installed by default) to parse the generated XML reports and display them in the Jenkins UI.
Example: Declarative Pipeline with Maven
Section titled “Example: Declarative Pipeline with Maven”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 } }}Example: Declarative Pipeline with Gradle
Section titled “Example: Declarative Pipeline with Gradle”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' ]) } }}Viewing Test Results
Section titled “Viewing Test Results”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.
Other Languages/Frameworks
Section titled “Other Languages/Frameworks”The pattern is similar for other ecosystems:
- JavaScript (Jest/Mocha): Run tests using
npm testoryarn test. Configure your test runner to output JUnit XML reports (e.g., usingjest-junit). Use thejunitstep in Jenkins. - .NET (NUnit/MSTest): Run tests using
dotnet test. Configure it to generate trx or JUnit XML reports. Use thejunitstep or dedicated plugins if available. - Python (pytest/unittest): Run tests using
pytestorpython -m unittest. Configure pytest to generate JUnit XML (pytest --junitxml=report.xml). Use thejunitstep.
Always check if your testing framework can produce reports in the JUnit XML format, as this is the most universally understood format by Jenkins.