我可以用--durations=1
打印最慢的测试。但是我怎样才能打印得最快呢?我认为答案与终端总结有关。但我需要以某种方式访问测试持续时间。我在类似的问题中发现了这段代码,但它不起作用。
def pytest_terminal_summary(terminalreporter, exitstatus, config):
for reps in terminalreporter.stats.values():
for rep in reps:
if rep.when == "call":
print("duration reported after all tests passed:", rep.nodeid, rep.duration)
我得到一个错误AttributeError: 'WarningReport' object has no attribute 'when'
我自己弄明白了:
def pytest_terminal_summary(terminalreporter, exitstatus, config):
time_value = []
for reps in terminalreporter.stats.values():
for rep in reps:
try:
if rep.when == "call":
time_value.append((rep.nodeid, rep.duration))
except AttributeError:
pass
print("Fastest test:", min(time_value, key=lambda x: x[1]))
输出:
Fastest test: ('tests/test_database.py::TestDatabase::test_method_5', 0.09348350000000005)