pytest-dev/pytest-cov

Ignore partial lines in the coverage report percentage

sebas031811 opened this issue · 3 comments

What's the problem this feature will solve?

Is there an option to ignore partial lines in the coverage report? Currently, we compare the coverage report with Codecov, which already ignores partial lines in the coverage report.
Currently, pytest treats a partial line as half of a covered line when calculating the coverage percentage in the report.

Describe the solution you'd like

We request a feature or flag to exclude partial lines when calculating coverage percentages in the report.

In the first image, we have an 86.67% of coverage for the file src/logger.py, but in the second image we have 84.62%

image

image

Alternative Solutions

There is an option # pragma: no branch to ignore lines in the coverage report. But we should put it in every partial line.

What do you mean by a partial line? Why do you want to ignore them? Maybe you don't want branch coverage at all, and just line coverage?

Hello! A 'partial line' refers to a situation in code coverage where only a portion of a line is covered by tests. This can happen, for instance, in an 'if' statement where only one branch is covered. The line containing the 'if' statement is marked as partial, contributing to a coverage percentage.

The reason for wanting to ignore partial lines is to align the behavior with other CI coverage reporters like Codecov. Codecov typically ignores these partial lines when calculating coverage percentages. When uploading the coverage report from pytest to Codecov, the differences in handling partial lines may lead to minor variations in coverage percentages between the two tools.

image

Interestingly, we've observed that using line coverage instead of branch coverage tends to magnify these differences. Ignoring partial lines can help maintain consistency and reduce discrepancies in coverage percentages across different reporting tools.

I don't know what you mean by "ignore the partial lines". I assume you mean you want to treat them as 100% executed. That's what line coverage would do. Can you show an example of how line coverage magnifies the differences?

FWIW, it is not a goal for coverage.py (the engine that pytest-cov wraps) to somehow mimic other tools' calculations precisely. I want to make it the most useful tool it can be in its own right. If one tool says 91% and another tool says 92%, you might have too many tools, or you have to accept that they have differences.