Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to fail pytest if test coverage falls under x%

Right now the way I'm running things is I have a set of test cases written with pytest that I run, if they fail then I fix and rework on. If they pass I use pytest-cov to get coverage and manually decide whether coverage is good enough. I was wondering if it was possible for pytest to fail if threshold for coverage is under x amount.

pytest --cov=myproject tests --cov-report=html
coverage report --fail-under=80
....
myproject/services/subnet.py                                                                        36     33     8%
myproject/severity.py                                                                                5      0   100%
--------------------------------------------------------------------------------------------------------------------
TOTAL                                                                                             8843   8739     1%
....
like image 332
Stupid.Fat.Cat Avatar asked Dec 20 '19 05:12

Stupid.Fat.Cat


2 Answers

You should use pytest for running tests, and failing if the tests fail. Then use coverage to assess the coverage amount, and fail if it is under:

pytest --cov=mypackage --cov-report= tests-or-whatever
coverage report --fail-under=80
like image 134
Ned Batchelder Avatar answered Sep 24 '22 15:09

Ned Batchelder


If you are using pytest-cov, you can use --cov-fail-under=MIN:

pytest --cov-fail-under=80 [...]
like image 26
Maximilian Hils Avatar answered Sep 23 '22 15:09

Maximilian Hils