coveragepy
coveragepy copied to clipboard
Separate fail_under option for branch and statement coverage
Is your feature request related to a problem? Please describe.
In our team, we want to assert that at least 80% branch coverage are reached. Meanwhile, statement coverage should also always be at 100%. However, fail_under = 80.0
only applies to the branch coverage if you enable the collection of it via branch = True
.
Describe the solution you'd like
Some option to set a threshold per coverage type wold be great. For example, by specifying branch = True, fail_under_statements = 100.0, fail_under_branch = 80.0
. Also, fail_under
could default to the current behavior (i.e. become an alias to branch coverage if it is enabled and else specify statement coverage).
Describe alternatives you've considered
- Writing a script that parses the output of coveragepy and realizes the same behavior. This is annoying since many different teams would have to do this on their own. Although one could also publish the (admittedly rather small) snippet in the docs of this library.
- Have a human check the output (e.g. in reviews).
Additional context Distantly related to #666.
I'm wondering if using the json report to drive some conditionals outside of coverage.py would be a simple way to get what you need.
Well, yeah. If you have the the above setup (collect coverage with branch=True, fail_under=80.0
), the tool will correctly report coverage being under 80%. The additional constraint on 100% statement coverage can be achieved like this (using Python 3):
coverage json
cat coverage.json | python -c "import json,sys;miss=json.load(sys.stdin)['totals']['missing_lines'];exit(0 if miss == 0 else 1);"
That works, yes. Thank you for the hint. You may still consider whether this is worth adding to the tool or its documentation.
~~While it works, it does a poor job at showing which lines are not statement-covered.~~
See https://nedbatchelder.com/blog/202111/coverage_goals.html for progress on improved target measurement.