Sphere
Log in Join
☆ 13

coverage.py says 98 percent but I know branches aren't tested

coverage 7.4, pytest-cov 4.1. The report shows 98% but looking at the HTML report, big if blocks show as covered even though only the true branch ever ran. I thought branch coverage was on. My .coveragerc has [run] branch = True but I invoke pytest --cov=mypkg — does that actually read .coveragerc? Something clearly isn't wired up the way I assumed.

⚑ Report

4 replies

☆ 0
V vim ·2026-10-01#1

pytest-cov reads .coveragerc by default, but if you also have a pyproject.toml with a [tool.coverage.run] section you might have conflicting config sources. Run coverage debug config to see what's actually active — that command shows the final merged configuration and where each value came from.

⚑ Report
☆ 0
S so ·2026-10-06#2

coverage debug config says branch = False. So it's not reading .coveragerc at all. The file is named .coveragerc in the project root, which I thought was the standard name. Is that wrong, or is something else taking precedence? There's no setup.cfg in the repo, which I would have suspected first.

⚑ Report
☆ 0
R route ·2026-10-12#3

The name is right. But if there's a setup.cfg with a [coverage:run] section, coverage prefers that. Or pytest-cov is being passed something that overrides. Run pytest --cov=mypkg --cov-branch to force it and see if the number changes — that tells you whether collection works at all.

⚑ Report
☆ 0
G green ·2026-10-22#4

Move everything into pyproject.toml under [tool.coverage.run] and [tool.coverage.report], then delete .coveragerc and any coverage sections in setup.cfg. Coverage's config discovery is a mess otherwise, and we wasted an afternoon on exactly this before consolidating. One config file, one source of truth, no surprises when someone upgrades pytest-cov.

⚑ Report
Log in to reply.