Stupid.Fat.Cat
Stupid.Fat.Cat

Reputation: 11295

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%
....

Upvotes: 26

Views: 17024

Answers (2)

Maximilian Hils
Maximilian Hils

Reputation: 6770

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

pytest --cov-fail-under=80 [...]

Upvotes: 31

Ned Batchelder
Ned Batchelder

Reputation: 375574

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

Upvotes: 23

Related Questions