How to setup test coverage reports in GitLab merge requests.

Adetoyese Kola-Balogun
2 min readJul 16, 2022
Photo by Pankaj Patel on Unsplash

The inclusion of a test job that validates your code in a CI/CD pipeline is quite typical. Users are notified if the tests fail and the pipeline fails. In order to resolve the tests that failed, the person working on the merge request must examine the job logs to determine where they occurred.

GitLab provides a report on the merge request, and you may configure your task to use unit test reports, making it simpler and quicker to find the failure without having to look through the full log. Only the JUnit report format is presently supported by unit test reports. The full Unit test reports are provided in the pipeline detail view if you don’t use merge requests but still want to review the output without looking through task logs.

Consider the below scenario;

Your project uses GitLab CI/CD, your default branch works perfectly, and your pipelines show that nothing is broken.

When a test fails and a member of your team submits a merge request, the pipeline displays the recognisable red indicator. The work logs, which typically have thousands of lines, must be examined further to determine the reason why the test failed.

As soon as you configure the unit test reports, GitLab gathers and makes them available in the merge request. No more looking through the employment logs.

Your process for developing and debugging becomes simpler, quicker, and more effective.

Setting up the test coverage reports

You must include artifacts:reports:junit and specify the paths to the generated test reports in.gitlab-ci.yml in order to allow Unit test reports in merge requests. If the reports are not.xml files, GitLab will return an error code 500.

Install jest-junit as a dependency in your project

npm install jest-junit

Configure the test stage in your .gitlab-ci.yml similarly to below

test:
stage: test
script:
- yarn test
artifacts:
reports:
junit:
- junit.xml
coverage_report:
coverage_format: cobertura
path: coverage/cobertura-coverage.xml

The task in the test step of the preceding example runs, and GitLab retrieves the job’s unit test report. The results are displayed in the merge request widget after the job has finished running, and the XML report is saved in GitLab as an artefact.

These reports can be read inside the pipeline’s information page if XML files in the JUnit report format are created and uploaded as part of a pipeline. A list of test suites and cases reported from the XML file is shown on this page’s Tests tab.

What to do if tests reports are emtpy

If the artefact containing the report expires, a unit test report may appear to be empty when viewed in a merge request. Set the report artifact’s expire in value to a later time if it commonly expires too soon.

--

--

Adetoyese Kola-Balogun

Software Developer | React Expert | Building financial software solutions.