【原文链接】Pytest如何查找用例耗时最长的N个

当存在大量测试脚本的时候,脚本的运行耗时就会变得很长,难免需要对脚本的运行时间进行分析,而要想提高自动化脚本的运行效率,最直接最有效的就是不断的去找出耗时最大的几个脚本,然后对其进行脚本性能优化提升,那么怎么找出耗时最大的N个脚本呢。

Pytest框架提供了这样的功能,即通过 –durations 参数即可对脚本的运行时间进行分析。编写如下10个测试脚本,每个测试脚本通过使用sleep模拟脚本耗时。

import timedef test_01():print("in test01...")time.sleep(1)assert 1==1def test_02():print("in test02...")time.sleep(2)assert 1==1def test_03():print("in test03...")time.sleep(3)assert 1==1def test_04():print("in test04...")time.sleep(4)assert 1==1def test_05():print("in test05...")time.sleep(5)assert 1==1def test_06():print("in test06...")time.sleep(6)assert 1==1def test_07():print("in test07...")time.sleep(7)assert 1==1def test_08():print("in test08...")time.sleep(8)assert 1==1def test_09():print("in test09...")time.sleep(9)assert 1==1def test_10():print("in test10...")time.sleep(10)assert 1==1

当使用 pytest –durations=0 的时候,会将所有脚本的耗时打印出来,执行结果如下,在执行结果的slowest durations 部分显示了所有用例的执行耗时。

(demo--ip5ZZo0) D:\demo>pytest -s --durations=0
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: D:\demo
plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 10 itemstest_demo.py in test01...
.in test02...
.in test03...
.in test04...
.in test05...
.in test06...
.in test07...
.in test08...
.in test09...
.in test10...
.==================== slowest durations ====================
10.01s call     test_demo.py::test_10
9.02s call     test_demo.py::test_09
8.01s call     test_demo.py::test_08
7.01s call     test_demo.py::test_07
6.02s call     test_demo.py::test_06
5.01s call     test_demo.py::test_05
4.01s call     test_demo.py::test_04
3.00s call     test_demo.py::test_03
2.00s call     test_demo.py::test_02
1.00s call     test_demo.py::test_01(20 durations < 0.005s hidden.  Use -vv to show these durations.)
=================== 10 passed in 57.40s ===================(demo--ip5ZZo0) D:\demo>

此时比如用例过多,我们只想找出耗时最大的五个脚本,此时只需要使用 pytest –durations=5 即可,执行结果如下,可以看出此时只显示了耗时最大的五个测试脚本的耗时,如果在实际开发应用中,则此时直接去分析这五个脚本的耗时原因,然后对其进行优化即可。

(demo--ip5ZZo0) D:\demo>pytest --durations=5
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: D:\demo
plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 10 itemstest_demo.py ..........                              [100%]=================== slowest 5 durations ===================
10.01s call     test_demo.py::test_10
9.02s call     test_demo.py::test_09
8.00s call     test_demo.py::test_08
7.01s call     test_demo.py::test_07
6.01s call     test_demo.py::test_06
=================== 10 passed in 55.14s ===================(demo--ip5ZZo0) D:\demo>

这里面有个注意点,就是当脚本耗时小于0.005秒时,在使用 –durations=0 的时候也是不会显示用例执行时间的,下面将脚本中的sleep语句全部删除,即如下:

def test_01():print("in test01...")assert 1==1def test_02():print("in test02...")assert 1==1def test_03():print("in test03...")assert 1==1def test_04():print("in test04...")assert 1==1def test_05():print("in test05...")assert 1==1def test_06():print("in test06...")assert 1==1def test_07():print("in test07...")assert 1==1def test_08():print("in test08...")assert 1==1def test_09():print("in test09...")assert 1==1def test_10():print("in test10...")assert 1==1

这里使用 –durations 执行完成后,结果也不会显示用例的执行耗时。

(demo--ip5ZZo0) D:\demo>pytest --durations=0
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: D:\demo
plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 10 itemstest_demo.py ..........                              [100%]==================== slowest durations ====================(30 durations < 0.005s hidden.  Use -vv to show these durations.)
=================== 10 passed in 2.19s ====================(demo--ip5ZZo0) D:\demo>

在执行的最后也提示了用例耗时小于0.005秒,那么如果说在这种情况下还是要显示用例的耗时时间,则可以继续加上 -vv 参数即可,执行结果如下所示:这里虽然显示了,但是都显示了0.00秒。因此在一般情况下,当耗时小于0.005秒时,基本就可以认为忽略不计了。

(demo--ip5ZZo0) D:\demo>pytest --durations=0 -vv
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 -- C:\Users\hitre\.virtualenvs\demo--ip5ZZo0\Scripts\python.exe
cachedir: .pytest_cache
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: D:\demo
plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 10 itemstest_demo.py::test_01 PASSED                         [ 10%]
test_demo.py::test_02 PASSED                         [ 20%]
test_demo.py::test_03 PASSED                         [ 30%]
test_demo.py::test_04 PASSED                         [ 40%]
test_demo.py::test_05 PASSED                         [ 50%]
test_demo.py::test_06 PASSED                         [ 60%]
test_demo.py::test_07 PASSED                         [ 70%]
test_demo.py::test_08 PASSED                         [ 80%]
test_demo.py::test_09 PASSED                         [ 90%]
test_demo.py::test_10 PASSED                         [100%]==================== slowest durations ====================
0.00s call     test_demo.py::test_01
0.00s call     test_demo.py::test_04
0.00s call     test_demo.py::test_05
0.00s call     test_demo.py::test_02
0.00s call     test_demo.py::test_07
0.00s call     test_demo.py::test_06
0.00s call     test_demo.py::test_10
0.00s call     test_demo.py::test_08
0.00s call     test_demo.py::test_03
0.00s call     test_demo.py::test_09
0.00s teardown test_demo.py::test_01
0.00s setup    test_demo.py::test_01
0.00s teardown test_demo.py::test_05
0.00s setup    test_demo.py::test_04
0.00s setup    test_demo.py::test_02
0.00s setup    test_demo.py::test_05
0.00s setup    test_demo.py::test_07
0.00s teardown test_demo.py::test_04
0.00s teardown test_demo.py::test_08
0.00s teardown test_demo.py::test_02
0.00s teardown test_demo.py::test_03
0.00s setup    test_demo.py::test_06
0.00s setup    test_demo.py::test_09
0.00s teardown test_demo.py::test_09
0.00s setup    test_demo.py::test_08
0.00s teardown test_demo.py::test_10
0.00s setup    test_demo.py::test_03
0.00s teardown test_demo.py::test_07
0.00s teardown test_demo.py::test_06
0.00s setup    test_demo.py::test_10
=================== 10 passed in 0.05s ====================(demo--ip5ZZo0) D:\demo>

此外,还可以同时指定 –durations=0 和 –durations-min=6 显示运行时间超过6秒的脚本。

(demo--ip5ZZo0) D:\demo>pytest --durations=0 --durations-min=6
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: D:\demo
plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 10 itemstest_demo.py ..........                              [100%]==================== slowest durations ====================
10.01s call     test_demo.py::test_10
9.02s call     test_demo.py::test_09
8.00s call     test_demo.py::test_08
7.01s call     test_demo.py::test_07
6.00s call     test_demo.py::test_06(25 durations < 6s hidden.  Use -vv to show these durations.)
=================== 10 passed in 55.15s ===================(demo--ip5ZZo0) D:\demo>

Pytest如何查找用例耗时最长的N个相关推荐

  1. java 火焰_利用火焰图查找java耗时最长的方法

    原标题:利用火焰图查找java耗时最长的方法 众所周知java方法有两种,一种是纯粹的java方法,另外一种是jni,java调用c的,方法声明是java的,实现并不是. 现在通过jni实现一些效率的 ...

  2. 最长不重复字符串python_Python简单实现查找一个字符串中最长不重复子串的方法...

    本文实例讲述了Python简单实现查找一个字符串中最长不重复子串的方法.,具体如下: 刚结束的一个笔试题,很简单,不多说简单贴一下具体的实现: #!usr/bin/env python #encodi ...

  3. python查找最长的字符串_Python简单实现查找一个字符串中最长不重复子串的方法...

    本文实例讲述了Python简单实现查找一个字符串中最长不重复子串的方法.分享给大家供大家参考,具体如下: 刚结束的一个笔试题,很简单,不多说简单贴一下具体的实现: #!usr/bin/env pyth ...

  4. python找最长的字符串_Python简单实现查找一个字符串中最长不重复子串的方法

    本文实例讲述了Python简单实现查找一个字符串中最长不重复子串的方法.分享给大家供大家参考,具体如下: 刚结束的一个笔试题,很简单,不多说简单贴一下具体的实现: #!usr/bin/env pyth ...

  5. 网易互娱耗时最长的活动

    网易互娱耗时最长的活动 package com.niukeoffer.a898;import java.util.Scanner; import java.util.Stack;public clas ...

  6. linux udt 源码,UDT linux下关闭链接耗时过长

    不知道使用UDT的人多不多,很大可能会石沉大海啊 ========================================================= 我在使用UDT库的时候,linux ...

  7. 开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?

    流媒体视频直播包括以下几个步骤:采集->处理->编码和封装->推流到服务器->服务器流分发->播放器流播放. 在流媒体处理编码的过程中,会有硬解码和软解码两种播放方式.两 ...

  8. Waiting for server respnse 耗时过长原因排查

    背景 开发了一个导入接口,测试过程中发现导入压缩包24M时,耗时50多秒.觉得这个时间太长了,可能存在问题,于是开始了漫长的排查之旅. 查看接口时间 通过Chrome DevTools 查看接口请求信 ...

  9. mysql备份耗时太长_Mysql数据不算大,备份却非常慢

    环境 硬件:DELL 1950, 146G SAS 15K RPMS * 2, 8G Ram 软件:2.6.9-55.ELsmp x86_64, mysql 5.1.x 现象 2个库,其中1个业务库下 ...

最新文章

  1. 文件包含中过滤了php,文件包含漏洞---php协议
  2. 双击jar运行main主函数实现-fatJAR
  3. Cloud一分钟 | 马云发表致股东的公开信;5G算什么?中国已着手研究6G相关工作...
  4. 闪退没由报错_关于floor()报错注入,你真的懂了吗?
  5. WCF服务端基于配置的实现——拦截
  6. python3 一 线程与互斥锁详解
  7. 年底清理垃圾了,整理了一整套python学习资料无偿送给大家
  8. 银行资产配置的新变化
  9. 线性代数1.51.5Cramer法则/克莱姆法则
  10. html插入activex,在HTML网页中插入ActiveX控件
  11. 恶意代码防范技术原理-计算机病毒和特洛伊木马分析与防护
  12. TIM_SetCompare1(TIM14,625); 但是这个办法对TIM4行不通。TIM4使用TIM_OCInitStructure.TIM_Pulse = dutyCycle;
  13. C#中向Chart中添加数据
  14. 小甲鱼老师《带你学C带你飞》的后续课程补充
  15. 怎么让上下两排对齐_《excel表中怎么使同一格内的上下两行对齐》 excel两表格数据对齐...
  16. ie上svg的兼容问题
  17. 2022年京东活动时间表,今年下半年何时活动力度最大?
  18. sdut 1309 不老的传说问题(区间DP,难,值得好好看)
  19. 后来者当仁不让 Android系统手机
  20. 淘客推广 25%高佣金产品旺季来袭

热门文章

  1. linux服务器远程修改mac地址,linux下修改MAC地址问题解决方法
  2. 计算机凭据分配在哪里,电脑策略没有凭据分配怎么办
  3. Moonbeam与Wormhole的Relayer Engine之间的跨链互连合约
  4. JSP实用教程——第二章JSP语法
  5. Android对应的版本号
  6. MongoDB基本操作(Nosql数据库入门与实践)
  7. 【bat】批处理教程之for的/f参数
  8. 从零开始Android游戏编程(第二版) 第二章 创建第一个程序Hello Tank
  9. 王学岗——————H265实现低延时投屏,从零实现高清无损投屏(对应第六节课)
  10. 如何在网页里插入3D模型并进行互动展示?