coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

Line coverage metrics do not add up for executed_lines and summary.covered_lines in generated coverage.json

Open JohnPeng47 opened this issue 1 year ago • 9 comments

Describe the bug Here below is an item in "files" generated from coverage.py:

"src\textual\widgets\_list_item.py": { "executed_lines": [ 1, 3, 5, 6, 7, 8, 11, 12, 19, 20, 22, 23, 25, 29, 32, 35, 36, 37 ], "summary": { "covered_lines": 15, "num_statements": 21, "percent_covered": 71.42857142857143, "percent_covered_display": "71", "missing_lines": 6, "excluded_lines": 0 }, "missing_lines": [ 26, 27, 30, 33, 38, 39 ], "excluded_lines": [],

I would expect len(executed_lines) == summary.covered_lines but the first is 18 and the later is 15. Where does the extra 3 come from?

To Reproduce I tried this with the following: pytest . --tb short --color no --cov=src\textual --cov-report json --continue-on-collection-errors --disable-warnings On the https://github.com/Textualize/textual repo.

Expected behavior My expectation is: covered_lines + executed_lines + excluded_lines == summary.num_statements, but this seems to not be the case?

JohnPeng47 avatar Dec 02 '24 16:12 JohnPeng47

I'd like to reproduce this, but I don't use poetry so I'm not sure how to get everything in order. Can you give the exact commands you used, starting with git clone?

nedbat avatar Dec 03 '24 12:12 nedbat

I think the three lines are the docstrings, but I'm not sure why they are treated differently.

nedbat avatar Dec 03 '24 13:12 nedbat

Yeah so here's the repo: https://github.com/JohnPeng47/textual-neutered

  1. Clone
  2. Install poetry in your current python env
  3. cd into repo and run poetry install to get deps
  4. run poetry shell to activate project venv with deps installed from 3
  5. run pytest . --tb short --color no --cov=src/textual --cov-report json --continue-on-collection-errors --disable-warnings
  6. [OPTIONAL] I have a script test_coverage.py that basically checks that this conditions is true for all coverage files: missing_check = len(missing_lines) + len(executed_lines) + len(excluded_lines) == total_stmts

JohnPeng47 avatar Dec 03 '24 14:12 JohnPeng47

Also a shorter example here:

"__init__.py": {
  "executed_lines": [
    0
  ],
  "summary": {
    "covered_lines": 0,
    "num_statements": 0,
    "percent_covered": 100,
    "percent_covered_display": "100",
    "missing_lines": 0,
    "excluded_lines": 0
  },
  "missing_lines": [],
  "excluded_lines": [],

In this example, both covered_lines and num_statements should be 1 right, not 0?

JohnPeng47 avatar Dec 03 '24 19:12 JohnPeng47

Also another thing that I just noticed now, is that the coverage collected on the same repo is different on Windows vs. Linux. Is this just inherent to the OS differences in settrace impl?

JohnPeng47 avatar Dec 04 '24 17:12 JohnPeng47

There is no OS difference in the settrace implementation. Likely the difference is do to OS differences in textual repo. If you don't think that's the case, let's look at some more specific details.

nedbat avatar Dec 04 '24 17:12 nedbat

Ahh no I think that makes sense. But could you confirm: len(missing_lines) + len(executed_lines) + len(excluded_lines) == total_stmts

Is invariant for every file under coverage?

JohnPeng47 avatar Dec 04 '24 17:12 JohnPeng47

Hey sorry to keep pestering, but do you think you can just answer that one question above? My coverage diffing code is broken for some reason, and I just want to be sure that the above condition should hold

JohnPeng47 avatar Dec 06 '24 15:12 JohnPeng47

But could you confirm: len(missing_lines) + len(executed_lines) + len(excluded_lines) == total_stmts

Is invariant for every file under coverage?

As I mentioned, docstrings complicate matters. I'm also not sure what number you mean by total_stmts. That's not a number in the JSON report.

nedbat avatar Jan 26 '25 13:01 nedbat