0
0
Jenkinsdevops~5 mins

Running unit tests in pipeline in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Unit tests check small parts of your code to make sure they work correctly. Running these tests automatically in a pipeline helps catch problems early before the code is shared or used.
When you want to verify that new code changes do not break existing features before merging.
When you want to automate testing so developers get quick feedback on their work.
When you want to ensure code quality by running tests on every code update.
When you want to prevent broken code from being deployed to production.
When you want to keep a history of test results for your project.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        stage('Run Unit Tests') {
            steps {
                sh 'mvn test'
            }
        }
    }
    post {
        always {
            junit '**/target/surefire-reports/*.xml'
        }
    }
}

This Jenkinsfile defines a pipeline with two main stages:

  • Checkout: Gets the latest code from the source control.
  • Run Unit Tests: Runs unit tests using Maven's mvn test command.

The post section collects test results using the junit step to show test reports in Jenkins.

Commands
This command updates or creates the Jenkins pipeline job named 'my-pipeline' using the Jenkinsfile in the project. It sets up the pipeline to run the defined stages.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline
Expected OutputExpected
Job 'my-pipeline' updated successfully
This command triggers the pipeline job 'my-pipeline' on the Jenkins server and waits for it to finish. It starts the process of running unit tests automatically.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-pipeline -w
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Checkout) [Pipeline] checkout ... (checkout logs) ... [Pipeline] } [Pipeline] stage [Pipeline] { (Run Unit Tests) [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ my-app --- [INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0 [Pipeline] } [Pipeline] junit Recording test results [Pipeline] End of Pipeline Finished: SUCCESS
-w - Waits for the job to complete before returning output
This command fetches the console output of the latest run of the 'my-pipeline' job to review the test execution details and results.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 console my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Checkout) [Pipeline] checkout ... (checkout logs) ... [Pipeline] } [Pipeline] stage [Pipeline] { (Run Unit Tests) [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ my-app --- [INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0 [Pipeline] } [Pipeline] junit Recording test results [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: automate running unit tests in your pipeline to catch errors early and keep your code healthy.

Common Mistakes
Not including the 'junit' step to publish test results in Jenkins.
Without publishing test results, Jenkins cannot show test reports or mark the build as unstable on test failures.
Always add a 'post' section with 'junit' to collect and display test results.
Running tests without checking out the latest code first.
Tests may run on old or missing code, causing confusion and invalid results.
Add a 'Checkout' stage before running tests to get the latest code.
Triggering the pipeline without waiting for it to finish when you want immediate feedback.
You won't see test results or know if the build succeeded until later.
Use the '-w' flag with Jenkins CLI build command to wait for completion.
Summary
Create a Jenkinsfile that checks out code and runs unit tests with a testing tool like Maven.
Use the 'junit' step to publish test results so Jenkins can show reports and mark build status.
Trigger the pipeline job and wait for it to finish to get immediate feedback on test success or failure.