1

When the average test coverage threshold across multiple modules isn't met, pytest doesn't fail with a non-zero exit code, even though it should.

My command:

❯ pytest --cov module1 --cov module2 --cov module3 --cov-report --cov-fail-under=75

I get the following result:

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 passes just fine even though my coverage threshold isn't met. This causes main 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.
5
  • You should report this as a bug. Commented Nov 11 at 9:37
  • 1
    Done and done! github.com/pytest-dev/pytest-cov/issues/728 Commented Nov 11 at 15:18
  • Probably not the answer you were looking for, but you could use the py2lcov tool from the lcov package, to convert your Coverage.py data to lcov format - then use lcov to aggregate and report on the result. Similarly, once converted, you can use teh genhtml tool to produce a navigable report. See py2lcov --help and the lcov and 'genhtml` man pages for details. The .../tests/py2lcov/ testcase shows some sample usage. Commented Nov 11 at 22:19
  • Thanks @HenryCox! Indeed not quite what I was looking for, but good to have some tricks up my sleeve. Commented Nov 13 at 16:21
  • Another consideration might be whether you need or want a uniform report for all the code in your project: Python, C/C++, Java, Verilog, etc. Commented Nov 14 at 17:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.