You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why doesn't pytest-cov fail with a non-zero exit code when code coverage threshold isn't met, when running on multiple directories? #728
FAIL Required test coverage of 75.0% not reached. Total coverage: 74.79%
But the exit code remains zero:
❯ echo$?
0
As a result, my CI/CD pipeline (triggered upon a PR to main) passes just fine even though my coverage threshold isn't met. This causes my main branch to have too low test coverage.
I've tried
Passing --cov=module1,module2,module3 instead of having one pytest call with three different --cov arguments. This causes the coverage report to fail altogether, because it says that CoverageWarning: Module module1,module2,module3 was never imported. I.e., it tries to import module1,module2,module3 as a single module and it fails, since they are separate modules.
Passing the to-be-included modules in pyproject.toml under [tool.coverage.run] in the include argument. Problem persists: test coverage threshold failure is noted, but exit code is zero.
Interestingly, when you test on each of the modules individually, you do get a non-zero exit code for the module that has too low coverage.
On the internet, I can't really seem to find a working solution. Copilot isn't very useful here either.
Thank you @samdewr for raising this. I also have this issue and found your comment on Stackoverflow, leading to this.
For anyone else coming across this issue, I worked around it in bash as follows, and thought it might be useful.
Put simply, I'm simultaneously sending the output to stdout and to a file using tee, and then looking for the error
message in the file using grep
pytest --cov module1 <other cov parameters> --cov-report --cov-fail-under=75 | tee pytestcov-output.txt
if grep -q 'FAIL Required test coverage of' pytestcov-output.txt; then
exit 1
fi
It looks like the rounding of the test coverage percentage is the issue. As e.g. in my example, the test coverage threshold isn't met (66.66% instead of the required 67%). Therefore, I rightfully see the message FAIL Required test coverage of 67% not reached. Total coverage: 66.67%. Nonetheless, the exit code seems to look at the rounded, zero-decimal percentage values of the test coverage vs. test coverage threshold, because the exit code is zero.
Activity
andywatkins-ancoris commentedon Nov 28, 2025
Thank you @samdewr for raising this. I also have this issue and found your comment on Stackoverflow, leading to this.
For anyone else coming across this issue, I worked around it in bash as follows, and thought it might be useful.
Put simply, I'm simultaneously sending the output to stdout and to a file using
tee, and then looking for the errormessage in the file using
grepwebknjaz commentedon Nov 28, 2025
@andywatkins-ancoris
filterwarnings = errorwould be more elegant, though.samdewr commentedon Dec 3, 2025
Hi both, thanks for your replies! I have been able to reproduce the issue (see also this StackOverflow post).
Update including MWE
I've been able to reproduce the bug. See this repo README.
It looks like the rounding of the test coverage percentage is the issue. As e.g. in my example, the test coverage threshold isn't met (66.66% instead of the required 67%). Therefore, I rightfully see the message
FAIL Required test coverage of 67% not reached. Total coverage: 66.67%. Nonetheless, the exit code seems to look at the rounded, zero-decimal percentage values of the test coverage vs. test coverage threshold, because the exit code is zero.