首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest文档50-命令行参数--durations统计用例运行时间

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

作者头像
上海-悠悠
发布2020-09-10 17:43:27
1.3K0
发布2020-09-10 17:43:27
举报

前言

写完一个项目的自动化用例之后,发现有些用例运行较慢,影响整体的用例运行速度,于是领导说找出运行慢的那几个用例优化下。 --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 ===========================

这样就可以对运行慢的用例针对性优化。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-09-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • —durations=N
  • —durations=0
  • —durations=3
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档