0
0
Jenkinsdevops~5 mins

Integration test stages in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Integration test stages help check if different parts of your software work well together before releasing it. They run automated tests that combine components to find problems early.
When you want to verify that your new code changes do not break how different modules interact.
When you need to run tests that require a database or external services to be available.
When you want to automate testing after building your application to catch integration bugs quickly.
When you want to run tests in a separate step after compiling your code but before deployment.
When you want to see clear reports of integration test results in your build pipeline.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the application...'
        sh 'mvn clean compile'
      }
    }
    stage('Integration Test') {
      steps {
        echo 'Running integration tests...'
        sh 'mvn verify -Pintegration-tests'
      }
      post {
        always {
          junit 'target/failsafe-reports/*.xml'
        }
      }
    }
  }
}

This Jenkinsfile defines a pipeline with two stages: Build and Integration Test.

The Build stage compiles the application using Maven.

The Integration Test stage runs integration tests with a Maven profile named integration-tests.

The post block collects test reports so Jenkins can display test results.

Commands
This command updates the Jenkins job configuration with the latest Jenkinsfile to include the integration test stage.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline
Expected OutputExpected
Job 'my-pipeline' updated successfully
--conf - Specifies the Jenkins configuration file to use
This command triggers a build of the Jenkins pipeline to run the integration test stage immediately.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-pipeline -f
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Build) Building the application... [Pipeline] sh [Pipeline] } [Pipeline] stage [Pipeline] { (Integration Test) Running integration tests... [Pipeline] junit Recording test results [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
-s - Specifies the Jenkins server URL
-f - Follows the build output live
This command fetches the JSON report of the latest integration test results from Jenkins.
Terminal
curl http://localhost:8080/job/my-pipeline/lastBuild/testReport/api/json
Expected OutputExpected
{"totalCount":10,"failCount":0,"skipCount":0,"suites":[{"name":"IntegrationTests","cases":[{"name":"testDatabaseConnection","status":"PASSED"}]}]}
Key Concept

If you remember nothing else from this pattern, remember: integration test stages run after build to verify that combined parts of your app work correctly together.

Common Mistakes
Running integration tests in the build stage instead of a separate integration test stage
This mixes concerns and makes it harder to isolate integration test failures from build issues.
Create a dedicated integration test stage after the build stage in the Jenkinsfile.
Not collecting test reports with the junit step
Without collecting reports, Jenkins cannot show test results or failures clearly in the UI.
Use the junit step in the post block to publish test results after running tests.
Triggering builds without updating the Jenkinsfile to include integration tests
The pipeline will not run integration tests if the Jenkinsfile does not define that stage.
Always update the Jenkins job configuration with the latest Jenkinsfile before triggering builds.
Summary
Define a separate integration test stage in your Jenkinsfile after the build stage.
Run integration tests using your build tool and collect test reports with the junit step.
Trigger the Jenkins pipeline build to run integration tests and verify results via Jenkins UI or API.