Display tests results in gitlab
28 January, 2021
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:
Live example: https://gitlab.com/demeringo/aws-s3-private-access/-/merge_requests/12
View the test details:
Live example: https://gitlab.com/demeringo/aws-s3-private-access/-/pipelines/248610524/test_report
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.
Next steps
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.
See https://docs.gitlab.com/ee/ci/unit_test_reports.html#viewing-junit-screenshots-on-gitlab
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
https://simplesaurus.com/posts/gitlab-ci-dependency-scanning-npm-audit/
Conclusion
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 !