0
0
Testing Fundamentalstesting~15 mins

Continuous testing in CI/CD in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify that the application builds and passes all tests automatically on each code commit
Preconditions (3)
Step 1: Commit and push a code change to the repository
Step 2: Open the CI/CD dashboard for the project
Step 3: Observe the pipeline start automatically
Step 4: Wait for the build and test stages to complete
Step 5: Check the test results in the pipeline report
✅ Expected Result: The pipeline triggers automatically on code commit, the build succeeds, all automated tests pass, and the test report shows no failures
Automation Requirements - Jenkins Pipeline with shell script and JUnit test reports
Assertions Needed:
Build stage completes successfully
Test stage completes successfully
Test report shows zero failed tests
Best Practices:
Use declarative pipeline syntax
Include explicit stages for build and test
Publish test results using JUnit plugin
Fail the pipeline if any test fails
Automated Solution
Testing Fundamentals
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
                sh 'mvn clean compile'
            }
        }
        stage('Test') {
            steps {
                echo 'Running automated tests...'
                sh 'mvn test'
            }
            post {
                always {
                    junit 'target/surefire-reports/*.xml'
                }
            }
        }
    }
    post {
        failure {
            echo 'Build or tests failed. Please check the logs.'
        }
        success {
            echo 'Build and tests passed successfully.'
        }
    }
}

This Jenkins pipeline script automates continuous testing in a CI/CD environment.

Agent any means the pipeline runs on any available agent.

There are two main stages: Build and Test.

In the Build stage, the command mvn clean compile compiles the application.

In the Test stage, mvn test runs automated tests.

The junit step publishes test results from the Surefire reports, so Jenkins can show test reports and fail the build if tests fail.

The post section handles messages on success or failure to inform the user.

This setup ensures that on every code commit, Jenkins builds the app, runs tests, and reports results automatically, which is the essence of continuous testing in CI/CD.

Common Mistakes - 3 Pitfalls
Not publishing test results with JUnit plugin
Using shell sleep commands to wait for build or tests
Not failing the pipeline on test failures
Bonus Challenge

Now add data-driven testing with 3 different input configurations in the test stage

Show Hint