您当前的位置: 首页 >  pytest

pytest文档50-命令行参数--durations统计用例运行时间

发布时间:2020-11-09 21:07:45 ,浏览量:5

前言

写完一个项目的自动化用例之后,发现有些用例运行较慢,影响整体的用例运行速度,于是领导说找出运行慢的那几个用例优化下。 –durations 参数可以统计出每个用例运行的时间,对用例的时间做个排序。

–durations=N pytest -h 查看命令行参数,关于 --durations=N 参数的使用方式

>pytest -h

reporting: --durations=N show N slowest setup/test durations (N=0 for all). 

当 N=0 的时候显示全部用例的运行时间

–durations=0

先写几个pytest的用例,在用例里面加sleep时间,这样方便看到每个用例运行的持续时间

import pytest
import time # 作者-上海悠悠 QQ交流群:717225969 # blog地址 https://www.cnblogs.com/yoyoketang/ @pytest.fixture() def set_up_fixture(): time.sleep(0.1) yield time.sleep(0.2) def test_01(set_up_fixture): print("用例1") time.sleep(1.0) def test_02(set_up_fixture): print("用例2") time.sleep(0.6) def test_03(set_up_fixture): print("用例3") time.sleep(1.2) def test_04(set_up_fixture): print("用例4") time.sleep(0.3) def test_05(set_up_fixture): print("用例5") time.sleep(2.3) 

当 N=0 的时候显示全部用例的运行时间

>pytest test_dur.py --durations=0 -v ============================= test session starts ============================= collected 5 items

test_dur.py::test_01 PASSED [ 20%] test_dur.py::test_02 PASSED [ 40%] test_dur.py::test_03 PASSED [ 60%] test_dur.py::test_04 PASSED [ 80%] test_dur.py::test_05 PASSED [100%] =========================== slowest test durations ============================ 2.30s call     test_dur.py::test_05 1.20s call     test_dur.py::test_03 1.00s call     test_dur.py::test_01 0.60s call     test_dur.py::test_02 0.30s call     test_dur.py::test_04 0.20s teardown test_dur.py::test_05 0.20s teardown test_dur.py::test_01 0.20s teardown test_dur.py::test_02 0.20s teardown test_dur.py::test_03 0.20s teardown test_dur.py::test_04 0.10s setup    test_dur.py::test_03 0.10s setup    test_dur.py::test_01 0.10s setup    test_dur.py::test_02 0.10s setup    test_dur.py::test_05 0.10s setup    test_dur.py::test_04 ========================== 5 passed in 7.05 seconds =========================== 

用例运行的时候会经历3个阶段:setup,call,teardown。call就是测试用例,setup和teardown就是用例的fixture部分。

–durations=3

如果我们只需要筛选出运行时间最慢的3条用例,可以设置–durations=3

>pytest test_dur.py --durations=3 -v ============================= test session starts ============================= collected 5 items

test_dur.py::test_01 PASSED [ 20%] test_dur.py::test_02 PASSED [ 40%] test_dur.py::test_03 PASSED [ 60%] test_dur.py::test_04 PASSED [ 80%] test_dur.py::test_05 PASSED [100%] ========================== slowest 3 test durations =========================== 2.30s call     test_dur.py::test_05 1.20s call     test_dur.py::test_03 1.00s call     test_dur.py::test_01 ========================== 5 passed in 7.00 seconds =========================== 

这样就可以对运行慢的用例针对性优化。 在这里插入图片描述 另外,欢迎加入软件测试技术交流群 313782132 ~进群可领取软件测试资料以及群内测试大牛解惑!

测试工程师职业发展路线图

功能测试 — 接口测试 — 自动化测试 — 测试开发 — 测试架构师

加油吧,测试人!如果你需要提升规划,那就行动吧,在路上总比在起点观望的要好。事必有法,然后有成。

资源不错就给个推荐吧~

关注
打赏
1688896170
查看更多评论

暂无认证

  • 5浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0716s