Python库中“跟踪”模块中的函数会生成程序执行的跟踪以及带注释的语句覆盖范围。它还具有列出在运行期间通过生成调用者关系调用的函数的功能。
下面以两个Python脚本为例来演示跟踪模块的功能。
#myfunctions.py import math def area(x): a = math.pi*math.pow(x,2) return a def factorial(x): if x==1: return 1 else: return x*factorial(x-1)
#mymain.py import myfunctions def main(): x = 5 print ('area=',myfunctions.area(x)) print ('factorial=',myfunctions.factorial(x)) if __name__=='__main__': main()
“跟踪”模块具有命令行界面。可以使用命令行开关来调用模块中的所有功能。最重要的选项是--trace,它在执行程序行时显示它们。在以下示例中,使用了另一个选项--ignore-dir。在生成跟踪时,它将忽略指定的目录。
E:\python37>python -m trace --ignore-dir=../lib --trace mymain.py
输出结果
mymain.py(2): def main(): mymain.py(7): if __name__=='__main__': mymain.py(8): main()--- modulename: mymain, funcname: main mymain.py(3): x=5 mymain.py(4): print ('area=',myfunctions.area(x)) --- modulename: myfunctions, funcname: area myfunctions.py(3): a=math.pi*math.pow(x,2) myfunctions.py(4): return a area= 78.53981633974483 mymain.py(5): print ('factorial=',myfunctions.factorial(x)) --- modulename: myfunctions, funcname: factorial myfunctions.py(6): if x==1: myfunctions.py(9): return x*factorial(x-1) --- modulename: myfunctions, funcname: factorial myfunctions.py(6): if x==1: myfunctions.py(9): return x*factorial(x-1) --- modulename: myfunctions, funcname: factorial myfunctions.py(6): if x==1: myfunctions.py(9): return x*factorial(x-1) --- modulename: myfunctions, funcname: factorial myfunctions.py(6): if x==1: myfunctions.py(9): return x*factorial(x-1) --- modulename: myfunctions, funcname: factorial myfunctions.py(6): if x==1: myfunctions.py(7): return 1 factorial= 120
--count选项生成用于与,盖延伸使用每个模块中的文件。
E:\python37>python -m trace --count mymain.py area= 78.53981633974483 factorial = 120
myfunctions.cover
1: import math 1: def area(x): 1: a = math.pi*math.pow(x,2) 1: return a 1: def factorial(x): 5: if x==1: 1: return 1 else: 4: return x*factorial(x-1)
mymain.cover
1: import myfunctions 1: def main(): 1: x = 5 1: print ('area=',myfunctions.area(x)) 1: print ('factorial=',myfunctions.factorial(x)) 1: if __name__=='__main__': 1: main()
如果还使用了–count选项,则--summary选项将显示简短摘要。
E:\python37>python -m trace --count --summary mymain.py area = 78.53981633974483 factorial = 120 lines cov% module (path) 8 100% myfunctions (E:\python37\myfunctions.py) 7 100% mymain (mymain.py)
在--file文件的选项,指定名称中累积计数在几个跟踪运行。
E:\python37>python -m trace --count --file report.txt mymain.py area = 78.53981633974483 factorial = 120 Skipping counts file 'report.txt': [Errno 2] No such file or directory: 'report.txt' E:\python37>python -m trace --count --file report.txt mymain.py area= 78.53981633974483 factorial= 120
--listfuncs选项显示程序执行期间调用的函数。
E:\python37>python -m trace --listfunc mymain.py | findstr -v importlib area= 78.53981633974483 factorial= 120 functions called: filename: E:\python37\lib\encodings\cp1252.py, modulename: cp1252, funcname: IncrementalEncoder.encode filename: E:\python37\myfunctions.py, modulename: myfunctions, funcname: <module> filename: E:\python37\myfunctions.py, modulename: myfunctions, funcname: area filename: E:\python37\myfunctions.py, modulename: myfunctions, funcname: factorial filename: mymain.py, modulename: mymain, funcname: <module> filename: mymain.py, modulename: mymain, funcname: main
--trackcalls选项与–list funcs选项一起使用。它生成呼叫关系。
E:\python37>python -m trace --listfunc --trackcalls mymain.py | findstr -v importlib area= 78.53981633974483 factorial= 120 calling relationships: --> E:\python37\myfunctions.py *** E:\python37\lib\trace.py *** --> mymain.py trace.Trace.runctx -> mymain.<module> *** E:\python37\myfunctions.py *** myfunctions.factorial -> myfunctions.factorial *** mymain.py *** mymain.<module> -> mymain.main --> E:\python37\lib\encodings\cp1252.py mymain.main -> cp1252.IncrementalEncoder.encode --> E:\python37\myfunctions.py mymain.main -> myfunctions.area mymain.main -> myfunctions.factorial