This blog post demonstrates how to add the following badges to a Python project on GitLab:
https://gitlab.com/patkennedy79/flask-stock-portfolio-codeThe ‘pipeline’ and ‘test coverage’ badges are the two badges that I like to include for each Python project that I have on GitLab. They provide a quick overview of the status of the project.
This blog post assumes that the Python project is using GitLab CI (Continuous Integration) to run a job that calculates the test coverage, such as using the pytest-cov
package.
Add the Badges
Start with a Python project in GitLab that doesn’t have any badges:
https://gitlab.com/patkennedy79/bildIn the left-hand navigation pane, scroll down to the bottom and select ‘Settings’ –> ‘General’:
Scroll down to the ‘Badges’ sections and click on ‘Expand’:
Add the ‘Pipeline Status’ badge:
The values to fill in to the three fields are:
- Name: Pipeline Status
- Link: https://gitlab.com/%{project_path}/-/commits/%{default_branch}
- Badge image URL: https://gitlab.com/%{project_path}/badges/%{default_branch}/pipeline.svg
Add the ‘Test Coverage’ badge:
The values to fill in to the three fields are:
- Name: Test Coverage
- Link: https://gitlab.com/%{project_path}/-/commits/%{default_branch}
- Badge image URL: https://gitlab.com/%{project_path}/badges/%{default_branch}/coverage.svg
At the bottom of the ‘Badges’ section, you should now see that both badges have been added:
However, the ‘Test Coverage’ badge is still reporting an ‘unknown’ coverage…
Fix Test Coverage Badge
The ‘Test Coverage’ badge uses a regex (Regular Expression) to parse the results of running a test coverage job in the CI (Continuous Integration) pipeline.
In the left-hand navigation pane, scroll down to the bottom and select ‘Settings’ –> ‘CI / CD’:
In the ‘General pipelines’ sections (should be the first section), click on ‘Expand’:
Scroll down to the ‘Test coverage parsing’ section:
If you are using pytest-cov
to run your coverage checks, then add the following regex and click ‘Save changes’:
- ^TOTAL.+?(\d+\%)$
This regex is extracting the total coverage from the last line in the ‘Coverage’ job that runs in the Gitlab CI pipeline:
Name Stmts Miss Cover Missing
-------------------------------------------------
bild/__init__.py 0 0 100%
bild/directory.py 47 1 98% 43
bild/file.py 88 14 84% 49, 77-82, 94-96, 102-103, 111-112
-------------------------------------------------
TOTAL 135 15 89%
For more information about how to parse other coverage reports:
https://docs.gitlab.com/ee/ci/pipelines/settings.html#test-coverage-parsing
In order to have the regex change take effect, a new commit needs to be made to the default branch for the project. The easiest way to do this is by committing an empty commit (no files changed):
$ git commit —allow-empty -m “Update coverage badge”
$ git push origin master
Wait for GitLab CI pipeline to run:
Once the pipeline completes, you should see the badges working in the ‘Project Overview’ section:
Conclusion
This blog post demonstrated the steps for adding badges to a Python project in GitLab.
from Planet Python
via read more
No comments:
Post a Comment