I'm working on a Laravel package project and setting up a pipeline to test it (against php 7.3, 7.4 and 8.0) on merge request and publishing it to my package registry on main branch.
I'm trying to set up a badge for code coverage, but i can't make it work correctly since it is always dipslayng unknown percentage.
stages:
- dependancies
- linting
- test
- publish
# Dependancies job
download dependancies:
stage: dependancies
# job stuff
# Linting jobs
phpcs:
stage: linting
# job stuff
# test jobs templates
.testing_template: &testing
stage: test
script:
- php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-text --colors=never
only:
- merge_requests
# Testing jobs
php 8:
image: mileschou/xdebug:8.0
<<: *testing
php 7.4:
image: mileschou/xdebug:7.4
<<: *testing
php 7.3:
image: mileschou/xdebug:7.3
<<: *testing
# Publish to package registry
publish:
only:
- master
- tags
# job stuff
So my pipeline works as follow:
So the part executed on the merge request is as follow

Moreover, i can see code coverage for each test job in the pipeline job section

(I'm not sure why the tests results in different code coverage percentages since the codebase is the same, but is obviously related to the php version and probably specific optimizations done in code parsing.)
Since this is a Laravel package project i'm testing it with PhpUnit, so i used the given regex for the test coverage parsing setting (taken from the Gitlab docs).
^\s*Lines:\s*\d+.\d+\%
I want a badge that show the code coverage status on the master branch, not on the feature branch, so i've created a coverage badge with the badge image url as follow (i added a reference to a specific test job, i.e. php 7.4)
https://gitlab.com/%{project_path}/badges/%{default_branch}/coverage.svg?job=php%207.4
but it keeps being displayed as
.
I've tought that Gitlab can't find the desired php 7.4 job since it is not executed on the master branch but on a feature branch, so i tried replacing the %{default_branch} placeholder with the %{commit_sha} to reference a specific commit, but this doesn't work neither since (i suppose) it reference the merge commit (that triggered just the publish job) and not the one that triggered the entire pipeline.
I cannot figure out how to get the code coverage percentage for the master branch.
I don't want to execute testing again on the master branche since i've already tested the feature branch before merging it.
How can i do? Am I missing something?
I was struggling with this too for a python project and found that the test coverage results is now deprecated (https://docs.gitlab.com/ee/ci/pipelines/settings.html#add-test-coverage-results-to-a-merge-request-deprecated)
The answer for me was to use the coverage keyword in my .gitlab-ci.yml with the required regex to extract the percentage from the job log
e.g.
test:
stage: test
extends: .python-reports
script:
- pipenv run pytest --cov --cov-report term-missing --cov-report xml:./coverage_out_report.xml --junitxml=./test_out_report.xml
coverage: '/TOTAL.*\s+(\d+%)$/'
And my badge defined as
Link https://gitlab.com/%{project_path}/-/commits/%{default_branch}
Badge image URL https://gitlab.com/%{project_path}/badges/%{default_branch}/coverage.svg
Now I see the coverage as 100% on main (or master) and a nice green badge
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With