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

前言

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

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

网易云完整视频课程《pytest+yaml 框架使用与开发》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338

posted @ 2020-09-02 21:05  上海-悠悠  阅读(2080)  评论(0编辑  收藏  举报