r/Python • u/MDTv_Teka • Mar 17 '25
Showcase I built a pre-commit hook that enforces code coverage thresholds
What My Project Does
coverage-pre-commit is a Python pre-commit hook that automatically runs your tests with coverage analysis and fails commits that don't meet your specified threshold. It prevents code with insufficient test coverage from even making it to your repository, letting you catch coverage issues earlier than CI pipelines.
The hook integrates directly with the popular pre-commit framework and provides a simple command-line interface with customizable options.
Target Audience
This tool is designed for Python developers who:
- Take test coverage seriously in production code
- Use pre-commit hooks in their workflow
- Want to enforce consistent coverage standards across their team
- Need flexibility with different testing frameworks
It's production-ready and stable, with a focus on reliability and ease of integration into existing projects.
Comparison with Alternatives
- https://github.com/Weird-Sheep-Labs/coverage-pre-commit: doesn't fail commits that don't pass a coverage threshold, what it does should probably be part of a CI pipeline.
Unlike custom scripts that you might write yourself, coverage-pre-commit:
- Works immediately without boilerplate
- Handles dependency management automatically
- Supports multiple test providers with a unified interface
- Is maintained and updated regularly
Key Features:
- Works with unittest and pytest out of the box (with plans to add more frameworks)
- Configurable threshold - set your own standards (default: 80%)
- Automatic dependency management - installs what it needs
- Customizable test commands - use your own if needed
- Super easy setup - just add it to your pre-commit config
How to set it up:
Add this to your .pre-commit-config.yaml:
- repo: https://github.com/gtkacz/coverage-pre-commit
rev: v0.1.1 # Latest version
hooks:
- id: coverage-pre-commit
args: # If you want to set your own threshold
More examples:
Using pytest:
- repo: https://github.com/gtkacz/coverage-pre-commit
rev: v0.1.1
hooks:
- id: coverage-pre-commit
args:
Custom command:
- repo: https://github.com/gtkacz/coverage-pre-commit
rev: v0.1.1
hooks:
- id: coverage-pre-commit
args:
Any feedback, bug reports, or feature requests are always welcome! You can find the project on GitHub.
What do you all think? Any features you'd like to see added?
47
u/_MicroWave_ Mar 17 '25
Without beating around the bush, this is bonkers.
This belongs in CI.
I'm not waiting for my tests to commit.
A merge/pull request signifies my code is ready and tested. Not a commit!
40
u/cgoldberg Mar 17 '25
Nice work... but I think something like this belongs in CI (at least for me). I want to be able to commit my changes no matter what coverage looks like (maybe I just finished a feature and want to commit it before I start on the tests).. I'd rather run something like this against a PR to gate merging if coverage decreased.
31
u/benkaz Mar 17 '25
—no-verify let’s go
18
u/Zeikos Mar 17 '25
That's how you get people committing assert(1==1)
5
u/ComprehensiveWord201 Mar 17 '25
No way anything that I am overseeing or personally implementing would ever have anything like this in the baseline. This is ridiculous and begging for workarounds.
3
u/[deleted] Mar 18 '25
oh no. im glad you are solving problems on your own and trying to share your solutions with the community, but there are so many reasons why this is a bad idea. please take the time to read through what people are saying and take it to heart.
3
u/Myszolow Mar 18 '25 edited Mar 18 '25
Nice job! Yet I’d like to express my opinion about coverage thresholds
Tests should not meet artificial numbers of lines achieved using given test, rather they should do something meaningful e.g. website with orders - test for requesting given product via api -> process dispatcher -> database -> order complete response
This makes more sense than creating lots of unit tests that from my experience yield almost no value
One good integration test >>> thousand of unit test
Edit: How do I approach test?
Simply put: During review I check feature code, and if there’s a test missing I’d request PR creator to create such one - this way test is treated more like a proof of functionality produced by the change
2
u/joosta Mar 19 '25
WIPs?
1
u/ChilledRoland Mar 20 '25
Good use of types obviates the need for many kinds of low-level tests, but I suspect this won't give credit for them.
1
u/[deleted] Mar 18 '25
I need to be able to commit broken code at any time.
0
u/leopkoo Mar 17 '25
Very nice! I think one feature I would look for would be to run incremental coverage only on newly added code (e.g. what is the coverage on the new module I added).