coverage-pre-commit exposes two pre-commit hooks:
coverage-xmlgenerates a coverage XML report.coverage-badgegenerates a code coverage badge using genbadge
This allows users to display a code coverage badge like in a repository README file without the need to rely on 3rd party tools.
A .coverage file is expected at the root of the project, generated by running coverage run directly or using a plugin such as pytest-cov when running tests.
Important
Make sure your .coverage file is up to date, otherwise the generated badge will display an incorrect percentage.
Add the following to your .pre-commit-config.yaml file:
- repo: https://github.com/Weird-Sheep-Labs/coverage-pre-commit
rev: 0.1.1
hooks:
- id: coverage-xml
- id: coverage-badgeThis will run the hooks with the default configuration, equivalent to:
- repo: https://github.com/Weird-Sheep-Labs/coverage-pre-commit
rev: 0.1.1
hooks:
- id: coverage-xml
args: ["-o", "reports/coverage/coverage.xml"]
- id: coverage-badge
args: ["-i", "reports/coverage/coverage.xml", "-o", "reports/coverage/coverage-badge.svg"]Assuming you have used the default configuration, you can then display the coverage badge in your README.md:
Important
The file paths to the generated XML file and badge must be trackable by git i.e not excluded in .gitignore.
In order to conform with the behaviour of the vast majority of pre-commit hooks, the tool will run on every available (installed) git hook type. However, in order to prevent frequent failed commits, it may be desirable to confine the tool execution hook type to git push by adding to each hook:
stage: ["pre-push"]Note
You will have to run pre-commit install --hook-type pre-push to activate pre-push hooks in your local repo.
Because pre-commit runs each set of hooks in isolated environments, it does not have access to your local virtual environment. Running tests with coverage (using coverage run pytest for example) requires using the dependencies in the virtual environment, making it impossible to run through pre-commit.
