Integrating TestPulse in Jenkins

In this section we will modify a Jenkins pipeline and add a new step that uploads test results to TestPulse.

For simplicity we will use a Java pipeline, but the upload step is not different for other languages!

Our initial pipeline looks like this:

pipeline {
    agent any

    stages {

        stage('Build and Test') {
            steps {
                script {
                    sh './gradlew clean test'
                }
            }
        }
    }
}

As you can see, we only have 1 step that runs ./gradlew clean test.

When Gradle runs, it will create test reports. We now need to upload these reports to TestPulse to analyze.

Set an environment variable

To authenticate your user, we need to set an environment variable named TESTPULSE_TOKEN. We can do this in different ways, but the simplest is to set the env variable in the node.

From Jenkins: Manage Jenkins –> Nodes –> Configure Node, under Node Properties, you can configure Environment Variables for the Node.

Create a new env variable named TESTPULSE_TOKEN, and add the token you copied from TestPulse in the previous step.

Add TestPulse upload step

Now that we set the env variable, we can authenticate and upload the test results.

Since our utility is written in python, you first need to setup a python environment (if not already setup!). Then, install our python utility called testpulse-uploader using pip install.

Finally, call testpulse-upload and pass all the necessary parameters. The full list of parameters is explained in its documentation.

For example:

  pipeline {
    agent any

    stages {
        stage('Build and Test') {
            steps {
                script {
                    sh './gradlew clean test'
                }
            }

            post {
              always {
                    sh """
                        # Setup python environment (only if you did not already set it up!)
                        apt install -y python3.12-venv
                        python3.12 -m venv venv
                        . venv/bin/activate

                        # Install testpulse-uploader
                        pip install testpulse-uploader

                        # Upload the results
                        testpulse-upload -tr "build/test-results/test/.*\\.xml" -cr "build/reports/jacoco/test/.*\\.xml" --config-file .testpulse.yaml
                    """
                }
            }
        }
    }
}

Worth mentioning:

  • make sure you modify the path to the test results and coverage files!
  • Since we want to always upload test results to TestPulse (even if there are failures!), you should put this step in the post of the test step, otherwise it will not run.

The uploader has many options. We suggest you to read its its documentation.

Push some code and check the results

Configuration-wise, you are done! The next time you run this job, the test results will be imported in TestPulse.