GitLab Advanced CI/CD - Hands-On Lab: Optimizing Pipeline Testing
The goal of this lab is to explore the different ways that we can configure testing in an application.
Estimated time to complete: 15 minutes
Objectives
- Stop a pipeline after failure
- Unit test reporting
In this lab, we will explore the different ways that we can configure testing in an application. Currently, we have the following testing setup for our project:
stages:
- deps
- test
default:
image: node:latest
install deps:
stage: deps
script:
- npm install jest
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules
test binarysearch:
stage: test
script:
- node_modules/.bin/jest binarysearch.test.js
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules
test linearsearch:
stage: test
script:
- node_modules/.bin/jest linearsearch.test.js
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules
This lab will explore how we can ensure a test pipeline when a single job fails. We will also see how we can add test reporting to our test jobs.
Task A. Stopping a Pipeline after Failure
In this example, let’s look at how we can cancel the pipeline in the case where one of our tests fails.
-
Navigate to your
Nodeproject repository. -
Select
.gitlab-ci.yml. -
Select Edit > Edit in pipeline editor.
-
Just below the stages section of your
.gitlab-ci.ymlfile, add a workflow to auto cancel the jobs.workflow: auto_cancel: on_job_failure: all -
With this configuration, if any jobs fail, the whole pipeline will fail. To test this, you can create a test job that will purposely fail.
test fail: stage: test script: - jet test.js -
Select Commit changes.
Let’s see how the pipeline handles the failed job.
-
In the left sidebar, select Build > Pipelines.
-
Select your most recent pipeline and observe the jobs. Note that when the
test failjob fails, other jobs cancel, showing a gray slash icon.Now that we have verified the auto cancel works, let’s remove the failing job.
-
Navigate to your repository.
-
Select
.gitlab-ci.yml. -
Select Edit > Edit in pipeline editor.
-
Remove the
test failjob. Your.gitlab-ci.ymlfile will look like this:stages: - deps - test workflow: auto_cancel: on_job_failure: all default: image: node:latest install deps: stage: deps script: - npm install jest cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test binarysearch: stage: test script: - node_modules/.bin/jest binarysearch.test.js cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test linearsearch: stage: test script: - node_modules/.bin/jest linearsearch.test.js cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules -
Select Commit changes.
Task B. Adding Test Reports
In this task, we will add a test report to our test jobs.
-
Ensure you are still in the Pipeline Editor (if not, navigate to Build > Pipeline Editor).
-
We are going to adjust our
jestcommands for thetest binarysearchandtest linearsearchjobs to add atestResultsProcessorto the command. We can do this by adding the--ci --testResultsProcessor=jest-junitflags to the command. The--ciflag will make Jest assume it is running in a CI environment. For this to work we also have to installjest-junitby adding it to ourinstall deps. Below is an example of the jobs after the changes have been made:install deps: stage: deps script: - npm install jest jest-junit cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test binarysearch: stage: test script: - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test linearsearch: stage: test script: - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules -
The test results need to be stored in a JUnit file in order to be accessed by the pipeline. To do so, we need to add the following code snippet to both of our tests after the
scriptkeyword:artifacts: when: always reports: junit: junit.xmlThe tests will now look like this:
test binarysearch: stage: test script: - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js artifacts: when: always reports: junit: junit.xml cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test linearsearch: stage: test script: - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js artifacts: when: always reports: junit: junit.xml cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules -
After making these changes, select Commit changes.
-
In the left sidebar, select Build > Pipelines.
-
Select your most recent pipeline.
-
Wait for the tests to complete. Refresh after the test jobs are complete and select the tab
Tests. -
You will see a report of your test results in the tab.
Lab Guide Complete
You have completed this lab exercise. You can view the other lab guides for this course.
Suggestions?
If you wish to make a change to the lab, please submit your changes via Merge Request.
db5b702f)
