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 ===========================

這樣就可以對運行慢的用例針對性優化。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM