簡介:pytest-stress允許在用戶定義的時間內(nèi)循環(huán)測試。特別適用于一些已知測試時間,但不知道運行次數(shù)的場景。
歷史攻略:
壓力測試工具:Stress詳解
Python:超過設(shè)定的時長則退出
安裝:
pip3 install pytest-stress
基礎(chǔ)案例:
Loop tests for 30 seconds:
$ pytest --seconds 30
Loop tests for 45 minutes:
$ pytest --minutes 45
Loop tests for 8 hours:
$ pytest --hours 8
Loop tests for 1 hour 8 minutes and 9 seconds:
$ pytest --hours 1 --minutes 8 --seconds 9
每次運行后增加運行等待:例如5秒,--delay 5
$ pytest --delay 5 --hours 4 --minutes 30
案例源碼:
# -*- coding: utf-8 -*-
# time: 2024/3/31 10:34
# file: test_demo.py
# 公眾號: 玩轉(zhuǎn)測試開發(fā)
import time
import sys
from logger import log
class TestDemo:
def test_01(self):
time.sleep(1)
log.info('test_case 01 run')
log.info(sys.platform)
assert(1 == 1)
def test_02(self):
time.sleep(1)
log.info('test_case 02 run')
log.info(sys.platform)
assert(2 == 2)
def test_03(self):
time.sleep(1)
log.info('test_case 03 run')
log.info(sys.version)
assert (3 == 3)
運行結(jié)果:pytest --seconds 10文章來源:http://www.zghlxwxcb.cn/news/detail-861384.html
(py397) D:\year2024>pytest --seconds 10
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.9.7, pytest-8.1.1, pluggy-1.4.0
rootdir: D:\year2024
configfile: pytest.ini
plugins: allure-pytest-2.13.3, anyio-3.6.2, dash-2.9.3, hypothesis-6.99.6, assume-2.4.3, cov-5.0.0, forked-1.6.0, html-4.1.1, metadata-3.1.1, repeat-0.9.3, rerunfailures-14.0, ssh-0.1, stress-1.0.1, xdist-3.5.0
collected 3 items
========================================================================================== Loop # 1 =========================================================================================
test_case\test_demo.py ... [100%]
========================================================================================== Loop # 2 =========================================================================================
. [100%]. [100%]. [100%]
========================================================================================== Loop # 3 =========================================================================================
. [100%]. [100%]. [100%]
========================================================================================== Loop # 4 =========================================================================================
. [100%]. [100%]. [100%]
=================================================================================== 12 passed in 12.68s ====================================================================================
(py397) D:\year2024>
delay的場景:pytest --seconds 10 --delay 3文章來源地址http://www.zghlxwxcb.cn/news/detail-861384.html
(py397) D:\year2024>pytest --seconds 10 --delay 3
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.9.7, pytest-8.1.1, pluggy-1.4.0
rootdir: D:\year2024
configfile: pytest.ini
plugins: allure-pytest-2.13.3, anyio-3.6.2, dash-2.9.3, hypothesis-6.99.6, assume-2.4.3, cov-5.0.0, forked-1.6.0, html-4.1.1, metadata-3.1.1, repeat-0.9.3, rerunfailures-14.0, ssh-0.1, stress-1.0.1, xdist-3.5.0
collected 3 items
========================================================================================== Loop # 1 =========================================================================================
test_case\test_demo.py ... [100%]
========================================================================================== Loop # 2 =========================================================================================
. [100%]. [100%]. [100%]
========================================================================================== Loop # 3 =========================================================================================
. [100%]. [100%]. [100%]
==================================================================================== 9 passed in 15.62s ====================================================================================
(py397) D:\year2024>
到了這里,關(guān)于pytest-stress:好用的pytest壓力測試插件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!