Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage erroneously reports partial coverage inside except block. #212

Closed
nedbat opened this issue Nov 15, 2012 · 4 comments
Closed

Coverage erroneously reports partial coverage inside except block. #212

nedbat opened this issue Nov 15, 2012 · 4 comments
Labels
branch bug Something isn't working

Comments

@nedbat
Copy link
Owner

nedbat commented Nov 15, 2012

Originally reported by CleanCut (Bitbucket: CleanCut, GitHub: CleanCut)


Coverage.py, version 3.5.3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11)
OS X 10.8

I have reproduced a situation where coverage erroneously reports a line as only being partially covered.

Please see the attache 'test.py' which you can use to duplicate the problem. The comments inside of test.py also discuss the issue in more depth.


@nedbat
Copy link
Owner Author

nedbat commented Jan 2, 2016

This is test.py:

#!python
#!/usr/bin/env python3

from unittest.mock import MagicMock

class X():
    def a(self):
        raise(Exception('expected'))

    def b(self):
        try:
            while True:
                var = 'somevalue'
                self.a()
        except Exception as e:
            if not (e.args == ('expected',)):
                raise(e)
            q = 1

x = X()

# Covers all of the a() function.
try:
    x.a()
except:
    pass

# Covers mot of the b() function, except "raise(e)", which should result
# in partial coverage of the if statement.
x.b()

# _Ought_ to cover the remainder of the b() function.  It does seem to record
# running "raise(e)" -- but the if statement is still marked as having
# only partial coverage.
x.a = MagicMock(side_effect=Exception('unexpected'))
try:
    x.b()
except:
    pass

# (This same bug may occur in Python 2.x -- I didn't try it there)
#
# To reproduce, first make sure that Python 3.3.0 is installed and the 'python3'
# binary is somewhere in your path.  Then install coverage under python3.
# Substitute the path to your python3 coverage executable for 'coverage3' below.
#
# coverage3 run --branch test.py && coverage3 html
#
# Then open htmlcov/index.html and click on 'test' to see the report.  It shows
# line 15 as portially covered.  But it was covered!
#
# Also, if you add a line outside the "if not" statement on line fifteen, then
# the line that you add is what gets marked as partially covered.
#
# Note that if you remove the 'while True' loop from line 11, then coverage
# reports properly.

@nedbat
Copy link
Owner Author

nedbat commented Jan 2, 2016

When I try this with coverage 3.7.1, it tells me that the "q = 1" line is partial because it never branches to the "except" line before it, which makes no sense. I'm working on a re-implementation of branch coverage which reports 100% for this file, so it will be fixed when that code is merged in.

@nedbat
Copy link
Owner Author

nedbat commented Jan 2, 2016

This will be fixed when the ast-branch is merged.

@nedbat
Copy link
Owner Author

nedbat commented Jan 8, 2016

This is fixed in 44719bd (bb), which will be 4.1

@nedbat nedbat closed this as completed Jan 8, 2016
@nedbat nedbat added minor bug Something isn't working branch labels Jun 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant