gpt4 book ai didi

python - python测试中的函数调用列表

转载 作者:行者123 更新时间:2023-11-28 21:12:48 25 4
gpt4 key购买 nike

我有一个对象

class Obj:
def method1(self):
print 'method1'

def method2(self):
print 'method2'

def method3(self):
print 'method3'

和函数

def do_something():
obj = Obj()
obj.method2()
obj.method1()
obj.method3()

我想编写测试 do_something 和 Obj 对象的测试。如何在不替换(模拟)和更改 obj 行为的情况下接收在 obj 上调用的方法列表?

有点像

['method2', 'method1', 'method3']

最佳答案

使用trace 包。请参阅文档:http://docs.python.org/2/library/trace.html

来自文档:

import sys
import trace

# create a Trace object, telling it what to ignore, and whether to
# do tracing or line-counting or both.
tracer = trace.Trace(
ignoredirs=[sys.prefix, sys.exec_prefix],
trace=0,
count=1)

# run the new command using the given tracer
tracer.run('main()')

# make a report, placing output in the current directory
r = tracer.results()
r.write_results(show_missing=True, coverdir=".")

关于python - python测试中的函数调用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17324633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com