Testing automatically is great, but only if you can see the results.
If your merge requests fails, you want to understand quickly what is wrong.
Inspired by an article from Adin Ermie, who wrote how to publish tfsec results with Azure Devops pipelines, I will show you how you can publish tests results in Gitlab. This will reduce your feedback loop and help you fix things faster.
Use xUnit as the result format
This is the trick.
Gitlab understand the xUnit/jUnit result format. It recognize it as test results and display it fine. If you already have automatic testing or static checks in your CI/CD chain, you are lucky. Many test frameworks or security checker can be configured to output results to a file in xUnit format.
Gitlab will recognize the output in xunit format and automagically display it in various places of your project.
View the test results directly in merge request:
View the test details:
How to do it in gitlab-ci
It is only 4 lines of code.
To publishing results you have modify your test steps in gitlab-ci.yml:
- output results in xUnit format (just use the correct flag for your framework)
- save output as an report artifact
- Gitlab displays it automatically
# Python tests example in .gitlab-ci.yml pytest: stage: test script: - pytest --junitxml=report.xml artifacts: when: always reports: junit: report.xml
This example if for python, but it works similarly for JS tests, TFSec or any other test where you can output xUnit reports.
You can have a look at for a JS example https://gitlab.com/demeringo/aws-s3-private-access/-/blob/master/.gitlab-ci.yml.
You can really display any kind of result, no reason to limit it to unit tests.
Display test result screenshots
If your tests generate screenshots, you can even configure gitlab to display them.
Show results of npm audit
Npm audit does not provide xunit output direcly, you may need to do something like this to integrate it with Gitlab
I showed you a easy way to reduce the development feedback loop. Having tests results as close as possible to your code should not take you long.
Try it !