[TIP] Coverage.py 4.1b2: re-written branch measurement

Ned Batchelder ned at nedbatchelder.com
Mon Jan 25 03:21:36 PST 2016


On 1/23/16 8:08 PM, Ned Batchelder wrote:
> I've just release Coverage.py 4.1b2: 
> https://pypi.python.org/pypi/coverage/4.1b2
>
> This version includes a completely rewritten branch analysis 
> implementation.  In 4.0 and before, the possible branches were 
> determined by examining the bytecode.  Now, we use the AST.  This has 
> resulted in a much more predictable assessment of possible branches, 
> closing a handful of long-standing bugs.
>
> But it also means that some things that were not considered possible 
> branches now are.  And in particular, they may be flagged as missing 
> branches.  If you use branch coverage, I would be interested in your 
> feedback.  There are some cases where it is not clear what the right 
> behavior is.
>
> For example:
>
>     try:                    # 1
>         ...                 # 2
>     except ValueError:      # 3
>         ...                 # 4
>     finally:                # 5
>         ...                 # 6
>
>     try:                    # 8
>         ...                 # 9
>     except:                 # 10
>         ...                 # 11
>     finally:                # 12
>         ...                 # 13
>
> In the first block of code, there's a branch from line 3 to line 6, in 
> the case of an exception other than ValueError.  Should coverage.py 
> flag that as missing if such an exception never happens?  Right now it 
> does.  In the second block of code, coverage.py understands that line 
> 10 can never jump to 13, because there is no exception that would skip 
> line 11.
>
I forgot to mention: Coverage.py gives you a missed branch for 
exceptions other than ValueError on line 3 above, but it won't if you 
don't have a finally clause and the exception leaves the function:

     def func():                 # 1
         try:                    # 2
             ...                 # 3
         except ValueError:      # 4
             ...                 # 5

On the face of it, that seems wrong: why should the presence of a 
finally clause change whether the except is considered to be partially 
covered or not?

--Ned.
> Try it, let me know what you think: 
> https://pypi.python.org/pypi/coverage/4.1b2
>
> --Ned.




More information about the testing-in-python mailing list