What cool hacks can be done using sys.settrace?

前端 未结 7 1669
無奈伤痛
無奈伤痛 2020-12-13 02:51

I love being able to modify the arguments the get sent to a function, using settrace, like :

import sys

def trace_func(frame,event,arg):
    va         


        
7条回答
  •  猫巷女王i
    2020-12-13 03:48

    Of course, code coverage is accomplished with the trace function. One cool thing we haven't had before is branch coverage measurement, and that's coming along nicely, about to be released in an alpha version of coverage.py.

    So for example, consider this function:

    def foo(x):
        if x:
            y = 10
        return y
    

    if you test it with this call:

    assert foo(1) == 10
    

    then statement coverage will tell you that all the lines of the function were executed. But of course, there's a simple problem in that function: calling it with 0 raises a UnboundLocalError.

    Branch measurement would tell you that there's a branch in the code that isn't fully exercised, because only one leg of the branch is ever taken.

提交回复
热议问题