测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----如何进行自动化脚本执行性能提升即脚本运行时间分析
收藏本文
作者:redrose2100 类别: 日期:2022-05-14 01:14:40 阅读:834 次 消耗积分:0 分
## 一、显示执行最慢的N个用例 (1)在test_demo.py中编写10个测试函数 ```python import time def test_demo01(): time.sleep(0.1) assert 1==1 def test_demo02(): time.sleep(0.2) assert 1==1 def test_demo03(): time.sleep(0.3) assert 1==1 def test_demo04(): time.sleep(0.4) assert 1==1 def test_demo05(): time.sleep(0.5) assert 1==1 def test_demo06(): time.sleep(0.6) assert 1==1 def test_demo07(): time.sleep(0.7) assert 1==1 def test_demo08(): time.sleep(0.8) assert 1==1 def test_demo09(): time.sleep(0.9) assert 1==1 def test_demo10(): time.sleep(1.0) assert 1==1 def test_demo11(): assert 1==1 ``` (2) 使用命令 pytest --durations=5 执行用来显示执行最慢的5个用例 ```bash $ pytest --durations=5 ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests\demo02 plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 11 items test_demo01.py ........... [100%] ========================================================================= slowest 5 durations ========================================================================== 1.00s call test_demo01.py::test_demo10 0.92s call test_demo01.py::test_demo09 0.81s call test_demo01.py::test_demo08 0.71s call test_demo01.py::test_demo07 0.60s call test_demo01.py::test_demo06 ========================================================================== 11 passed in 5.61s ========================================================================== ``` ## 二、显示所有用例的执行时间 (1)使用pytest --durations=0 即可显示所有用例的执行时间 ```bash $ pytest --durations=0 ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests\demo02 plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 11 items test_demo01.py ........... [100%] ========================================================================== slowest durations =========================================================================== 1.01s call test_demo01.py::test_demo10 0.92s call test_demo01.py::test_demo09 0.80s call test_demo01.py::test_demo08 0.71s call test_demo01.py::test_demo07 0.60s call test_demo01.py::test_demo06 0.51s call test_demo01.py::test_demo05 0.40s call test_demo01.py::test_demo04 0.31s call test_demo01.py::test_demo03 0.20s call test_demo01.py::test_demo02 0.12s call test_demo01.py::test_demo01 (23 durations < 0.005s hidden. Use -vv to show these durations.) ========================================================================== 11 passed in 5.63s ========================================================================== ``` (2)细心的发现上面并没有显示所有的,test_demo11用例没有显示,这是因为pytest默认显示的耗时大于0.005秒的用例,小于0.005秒的用例默认是不显示的,如果要显示耗时小于0.005秒的用例,需要加上 -vv的参数 ```bash $ pytest --durations=0 -vv ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Python39\python.exe cachedir: .pytest_cache rootdir: D:\src\blog\tests\demo02 plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 11 items test_demo01.py::test_demo01 PASSED [ 9%] test_demo01.py::test_demo02 PASSED [ 18%] test_demo01.py::test_demo03 PASSED [ 27%] test_demo01.py::test_demo04 PASSED [ 36%] test_demo01.py::test_demo05 PASSED [ 45%] test_demo01.py::test_demo06 PASSED [ 54%] test_demo01.py::test_demo07 PASSED [ 63%] test_demo01.py::test_demo08 PASSED [ 72%] test_demo01.py::test_demo09 PASSED [ 81%] test_demo01.py::test_demo10 PASSED [ 90%] test_demo01.py::test_demo11 PASSED [100%] ========================================================================== slowest durations =========================================================================== 1.01s call test_demo01.py::test_demo10 0.91s call test_demo01.py::test_demo09 0.81s call test_demo01.py::test_demo08 0.71s call test_demo01.py::test_demo07 0.60s call test_demo01.py::test_demo06 0.51s call test_demo01.py::test_demo05 0.40s call test_demo01.py::test_demo04 0.30s call test_demo01.py::test_demo03 0.21s call test_demo01.py::test_demo02 0.11s call test_demo01.py::test_demo01 0.00s setup test_demo01.py::test_demo01 0.00s setup test_demo01.py::test_demo06 0.00s setup test_demo01.py::test_demo08 0.00s setup test_demo01.py::test_demo10 0.00s teardown test_demo01.py::test_demo01 0.00s teardown test_demo01.py::test_demo11 0.00s setup test_demo01.py::test_demo04 0.00s teardown test_demo01.py::test_demo02 0.00s teardown test_demo01.py::test_demo07 0.00s teardown test_demo01.py::test_demo10 0.00s teardown test_demo01.py::test_demo03 0.00s teardown test_demo01.py::test_demo04 0.00s teardown test_demo01.py::test_demo08 0.00s teardown test_demo01.py::test_demo06 0.00s setup test_demo01.py::test_demo09 0.00s setup test_demo01.py::test_demo02 0.00s teardown test_demo01.py::test_demo09 0.00s setup test_demo01.py::test_demo05 0.00s setup test_demo01.py::test_demo03 0.00s setup test_demo01.py::test_demo07 0.00s setup test_demo01.py::test_demo11 0.00s teardown test_demo01.py::test_demo05 0.00s call test_demo01.py::test_demo11 ========================================================================== 11 passed in 5.63s ========================================================================== ``` ## 三、显示所有耗时大于t秒的用例 (1)使用 pytest --durations=0 --durations-min=0.5 即可显示所有耗时大于0.5秒的用例 ```bash $ pytest --durations=0 --durations-min=0.5 ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests\demo02 plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 11 items test_demo01.py ........... [100%] ========================================================================== slowest durations =========================================================================== 1.01s call test_demo01.py::test_demo10 0.91s call test_demo01.py::test_demo09 0.81s call test_demo01.py::test_demo08 0.71s call test_demo01.py::test_demo07 0.61s call test_demo01.py::test_demo06 0.51s call test_demo01.py::test_demo05 (27 durations < 0.5s hidden. Use -vv to show these durations.) ========================================================================== 11 passed in 5.63s ========================================================================== ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/198
上一篇:
Pytest----如何安装和使用插件
下一篇:
Pytest----多进程并行执行自动化测试脚本
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件