coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

Covered branch inside finally block showing up as missing

Open nedbat opened this issue 7 years ago • 2 comments

Originally reported by David MacIver (Bitbucket: david_maciver_, GitHub: Unknown)


In some work I'm doing on Hypothesis, coverage is currently showing up some branches as missing that are covered.

Unfortunately I do not have a minimized reproduction. I tried a moderate amount and couldn't seem to trigger the behaviour in anything other than the actual example.

The following script will run a reproduction of this:

#!/usr/bin/env bash

git clone https://github.com/HypothesisWorks/hypothesis-python.git
cd hypothesis-python
git checkout [db1db2719cd09cfe5c565967667aca05bf8ad615 (bb)](https://bitbucket.org/ned/coveragepy/commits/db1db2719cd09cfe5c565967667aca05bf8ad615)
virtualenv v
source v/bin/activate
pip install coverage attrs pytest-xdist
export HYPOTHESIS_INTERNAL_COVERAGE=true
export PYTHONPATH=src
python -m coverage run  --branch --include=src/hypothesis/core.py -m pytest tests/cover/test_reproduce_failure.py  -n 0 --ff
coverage report --rcfile=/dev/null --show-missing

You will see a lot of things missing here because this isn't the full set of tests for that file, but the notable ones are "811->829, 824->829". Both of these are definitely covered - if you add an "else assert False" to the if branches then that assert will fire in these tests - but the report shows them as missing branches.

I've tested this on both Python 2.7.13 and 3.6.0 and it seems to behave the same way in each.

NB: You may notice that this file does a bunch of messing around with coverage and tracing. All of that should be turned off by the HYPOTHESIS_INTERNAL_COVERAGE=true environment variable, so I'm reasonably sure that's not the culprit here.


  • Bitbucket: https://bitbucket.org/ned/coveragepy/issue/623

nedbat avatar Dec 15 '17 11:12 nedbat

Original comment by David MacIver (Bitbucket: david_maciver_, GitHub: Unknown)


For added confusingness, AFAICT the second but not the first of these two lines cannot be pragma-ed away! Adding a pragma: no branch to it causes it to still show up as an uncovered branch.

nedbat avatar Dec 15 '17 13:12 nedbat

@nedbat I can confirm that this is some combination of fixed, and a misunderstanding around #867.

You can see in https://github.com/HypothesisWorks/hypothesis/commit/f523ae5d709fe3dea2b8c5b2f68a620608d53a15 that the surviving branch was "if-true in finally only occurs when raising an exception", which is perhaps unsurprising in Hypothesis' engine code to replay failing examples. I've refactored the code instead of trying to explain the coverage branches here, but I think this issue is fully subsumed by #867 and can be closed.

Zac-HD avatar Apr 30 '22 04:04 Zac-HD