1

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

There are two files that are being tested:

  • example_solution.py
  • created_unit_test.py

"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

mkrieger1
24.2k7 gold badges68 silver badges84 bronze badges
asked Jul 30, 2024 at 15:43
0

1 Answer 1

0

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

answered Jul 31, 2024 at 11:04
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I used coverage.py in a venv and didn't notice that somehow an old version was installed. After updating everything worked as intendet.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.