pythoncode-coveragecoverage.py

coverage.py always reports one missing statement but the html report doesen't show what the missing statement is


I am using coverage.py to measure the statement/code coverage of my test.

There are two files that are being tested:

"example_solution" contains a function "contains_negative()" that should return a boolean if any negative number is found within a list. "created_unit_test.py" is an automatically generated unit-test for "example_solution".

I run that code in my linux cli: coverage run --branch created_scripts/created_unit_test.py

And receive the report in ASCII form:

----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
Name                                    Stmts   Miss Branch BrPart  Cover
------------------------------------------------------------------------
created_scripts/created_unit_test.py      12      1      0      0    92%
created_scripts/example_solution.py        3      1      2      0    80%
------------------------------------------------------------------------
TOTAL                                     15      2      2      0    88%

But the HTML reports, as shown in the images below, don't show where that one missed statement is.

My questions are:

  1. What is the one missing statement that doesn't get highlighted and how do I get rid of it?
  2. How does coverage.py calculate the score? As an example, "example_solution.py" has 3 statements, 1 seemingly is not covered. So one would expect a statement coverage score of 66%, but why is it 80%?

This is the HTML report of the unit-test not highlighting the missed statement

Other HTML report but for the code with the same issue


Solution

  • The reports look very odd, but I also notice they are using coverage 5.5, which is quite old. Can you try with an updated version of coverage?

    As to how the total is calculated: https://coverage.readthedocs.io/en/7.6.0/faq.html#q-how-is-the-total-percentage-calculated