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

Chris Jerdonek chris.jerdonek at gmail.com
Mon Jan 25 09:01:59 PST 2016


On Mon, Jan 25, 2016 at 3:21 AM, Ned Batchelder <ned at nedbatchelder.com> wrote:
> On 1/23/16 8:08 PM, Ned Batchelder wrote:
>>
>> 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?

Perhaps the answer could be: because coverage.py only considers
branches _within_ functions (i.e. from one line to another).  In
particular, it doesn't consider as branches the possibility of an
arbitrary line of code raising an exception and leaving the function
without being handled.

For example, in the following lines, an exception being raised at #1
isn't (I think) considered a distinct branch.  (There is no
surrounding exception handling in this example.):

    ...  #1
    ...  #2

One reason this makes sense is that maybe in general it's not possible
for an arbitrary line of code to raise an exception, and so it would
be wrong to consider that branch missing.

Come to think of it, that could be a better answer to the question
above.  If you have a finally clause, it should be only because it is
needed.  If an exception other than ValueError can't occur, then the
finally clause can (and should?) be removed because it's not needed.

--Chris



>
> --Ned.
>
>> Try it, let me know what you think:
>> https://pypi.python.org/pypi/coverage/4.1b2
>>
>> --Ned.
>
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python



More information about the testing-in-python mailing list