1. Ned Batchelder
  2. coverage.py
Issue #212 resolved

Coverage erroneously reports partial coverage inside except block.

CleanCut
created an issue

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.

Comments (4)

  1. Ned Batchelder repo owner

    This is test.py:

    #!/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.
    
  2. Ned Batchelder repo owner

    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.

  3. Log in to comment