python - 使用 pytest 在控制台中打印最快的测试
问题描述
我可以使用 打印最慢的测试--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)
推荐阅读
- node.js - node-typescript 应用程序 docker 命令错误
- css - 使用 display flex 水平对齐项目
- c++ - 有没有办法删除结构数组的一个元素?
- oracle - 如何知道谁以及如何在 Oracle 自治事务处理中使用了预定义的数据库服务名称
- asp.net-core-mvc - 身份服务器 4 loclaAPi 频繁报错:响应状态码不表示成功:503(服务器已关闭)
- javascript - 应该为点赞按钮使用哪种 HTTP 请求方法
- java - java的compareTo()函数中的字符层次结构
- python - 无法使用 seaborn 绘制双轴
- terraform - 在 terragrunt 配置之间共享 aws_route53_record?
- laravel - 从 Vue 发布到 Laravel 时,Laravel 重定向不起作用