Bird
Raised Fist0
Jenkinsdevops~5 mins

Integration test stages in Jenkins - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of an integration test stage in a Jenkins pipeline?
easy
A. To check if different parts of the software work together correctly
B. To deploy the software to production
C. To compile the source code
D. To clean up temporary files after deployment

Solution

  1. Step 1: Understand the role of integration tests

    Integration tests verify that different modules or components of software interact correctly.
  2. Step 2: Identify the stage purpose in Jenkins

    In Jenkins pipelines, integration test stages run after building the code but before deployment to ensure combined parts work well.
  3. Final Answer:

    To check if different parts of the software work together correctly -> Option A
  4. Quick Check:

    Integration test purpose = verify combined parts work [OK]
Hint: Integration tests check combined parts, not deployment or build [OK]
Common Mistakes:
  • Confusing integration tests with deployment steps
  • Thinking integration tests compile code
  • Assuming integration tests clean files
2. Which of the following is the correct syntax to define an integration test stage in a Jenkins declarative pipeline?
easy
A. stage: 'Integration Test' steps: sh 'run-tests.sh'
B. integrationTest { steps { sh 'run-tests.sh' } }
C. stage('Integration Test') { steps { sh 'run-tests.sh' } }
D. testStage('Integration Test') { shell 'run-tests.sh' }

Solution

  1. Step 1: Recall Jenkins declarative pipeline stage syntax

    The correct syntax uses stage('Name') { steps { ... } } to define stages.
  2. Step 2: Match the options to syntax

    stage('Integration Test') { steps { sh 'run-tests.sh' } } matches the correct syntax with stage('Integration Test') { steps { sh 'run-tests.sh' } }.
  3. Final Answer:

    stage('Integration Test') { steps { sh 'run-tests.sh' } } -> Option C
  4. Quick Check:

    Jenkins stage syntax = stage('name') { steps { ... } } [OK]
Hint: Jenkins stages use stage('name') { steps { ... } } syntax [OK]
Common Mistakes:
  • Using incorrect keywords like integrationTest or testStage
  • Missing curly braces or wrong colon usage
  • Using shell instead of sh for shell commands
3. Given this Jenkins pipeline snippet, what will be the output if the integration tests fail?
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Building...' }
    }
    stage('Integration Test') {
      steps { sh 'exit 1' }
    }
    stage('Deploy') {
      steps { echo 'Deploying...' }
    }
  }
}
medium
A. The pipeline retries Integration Test automatically
B. The pipeline continues to Deploy stage despite failure
C. The pipeline skips Build and runs Deploy only
D. The pipeline stops after the Integration Test stage with failure

Solution

  1. Step 1: Understand shell command exit codes in Jenkins

    The command sh 'exit 1' simulates a failure because exit code 1 means error.
  2. Step 2: Jenkins pipeline behavior on failure

    By default, Jenkins stops the pipeline on any stage failure and does not proceed to next stages.
  3. Final Answer:

    The pipeline stops after the Integration Test stage with failure -> Option D
  4. Quick Check:

    Failure exit code stops pipeline [OK]
Hint: Exit code 1 in sh step causes pipeline failure and stop [OK]
Common Mistakes:
  • Assuming pipeline continues after failure
  • Thinking Jenkins retries failed stages automatically
  • Confusing exit 0 (success) with exit 1 (failure)
4. You wrote this Jenkins pipeline stage for integration tests:
stage('Integration Test') {
  steps {
    sh 'run-integration-tests.sh'
  }
}
But the pipeline fails with a syntax error. What is the likely cause?
medium
A. Missing steps block inside the stage
B. The pipeline is missing the pipeline block wrapper
C. Incorrect indentation of the sh command
D. The stage keyword is misspelled

Solution

  1. Step 1: Check Jenkins declarative pipeline structure

    Every declarative pipeline must start with a pipeline { ... } block wrapping all stages.
  2. Step 2: Identify missing pipeline block

    The snippet shows only a stage block without the required pipeline wrapper, causing syntax error.
  3. Final Answer:

    The pipeline is missing the pipeline block wrapper -> Option B
  4. Quick Check:

    Declarative pipelines need pipeline { stages { ... } } [OK]
Hint: Always wrap stages inside pipeline { } block [OK]
Common Mistakes:
  • Assuming indentation causes syntax error
  • Forgetting the pipeline block wrapper
  • Misspelling stage keyword (not shown here)
5. You want to add an integration test stage that runs only if the build stage succeeds and skips deployment if tests fail. Which Jenkins pipeline snippet correctly implements this logic?
medium
A. stage('Integration Test') { steps { sh 'run-tests.sh' } } stage('Deploy') { steps { sh 'deploy.sh' } }
B. stage('Integration Test') { steps { sh 'run-tests.sh' } post { failure { error 'Tests failed, stop pipeline' } } } stage('Deploy') { steps { sh 'deploy.sh' } }
C. stage('Integration Test') { steps { sh 'run-tests.sh || true' } } stage('Deploy') { steps { sh 'deploy.sh' } }
D. stage('Integration Test') { steps { sh 'run-tests.sh' } } stage('Deploy') { when { expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' } } steps { sh 'deploy.sh' } }

Solution

  1. Step 1: Ensure integration tests run after build and affect deployment

    The integration test stage runs normally after build; deployment should only happen if tests pass.
  2. Step 2: Identify the correct sequential stage implementation

    In Jenkins pipelines, stages run sequentially by default. A failure in 'Integration Test' (non-zero exit from sh) causes the pipeline to fail and skip the 'Deploy' stage. stage('Integration Test') { steps { sh 'run-tests.sh' } } stage('Deploy') { steps { sh 'deploy.sh' } } correctly implements this logic.
  3. Final Answer:

    stage('Integration Test') { steps { sh 'run-tests.sh' } } stage('Deploy') { steps { sh 'deploy.sh' } } -> Option A
  4. Quick Check:

    Sequential stage failure skips subsequent stages [OK]
Hint: Default sequential execution stops pipeline on test failure, skipping deploy [OK]
Common Mistakes:
  • Ignoring test failure and always deploying
  • Using post failure to stop pipeline but not gating deploy
  • Forcing tests to pass with || true, hiding failures
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard