栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Pytest实用技巧(1)如何进行自动化脚本执行性能提升即脚本运行时间分析

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pytest实用技巧(1)如何进行自动化脚本执行性能提升即脚本运行时间分析

更多更详细内容请参考:Pytest官方文档深入解读(2)如何执行pytest脚本 目录
  • 显示执行最慢的N个用例
  • 显示所有用例的执行时间
  • 显示所有耗时大于t秒的用例
正文 显示执行最慢的N个用例

(1)在test_demo.py中编写10个测试函数

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个用例

$ 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:srcblogtestsdemo02
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 即可显示所有用例的执行时间

$ 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:srcblogtestsdemo02
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的参数

$ 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:Python39python.exe
cachedir: .pytest_cache
rootdir: D:srcblogtestsdemo02
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秒的用例

$ 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:srcblogtestsdemo02
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 ==========================================================================

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/681784.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号