pytest-stress:好用的pytest压力测试插件

简介:pytest-stress允许在用户定义的时间内循环测试。特别适用于一些已知测试时间,但不知道运行次数的场景。

历史攻略:

压力测试工具:Stress详解

Python:超过设定的时长则退出

安装:

复制代码
pip3 install pytest-stress

基础案例:

复制代码
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
# 公众号: 玩转测试开发
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)

运行结果:pytest --seconds 10

复制代码
(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

复制代码
(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>
相关推荐
我的xiaodoujiao1 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 18--测试框架Pytest基础 2--插件和参数化
python·学习·测试工具·pytest
小小测试开发2 天前
pytest 库用法示例:Python 测试框架的高效实践
开发语言·python·pytest
know__ledge2 天前
Pytest+requests进行接口自动化测试8.0(Allure进阶 + 文件上传接口 + 单接口多用例)
pytest
川石课堂软件测试2 天前
CSS中常用的几种定位。
开发语言·css·python·网络协议·http·html·pytest
h7997102 天前
go资深之路笔记(八) 基准测试
golang·压力测试
啊森要自信3 天前
【GUI自动化测试】Python 自动化测试框架 pytest 全面指南:基础语法、核心特性(参数化 / Fixture)及项目实操
开发语言·python·ui·单元测试·pytest
啊森要自信4 天前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash
我的xiaodoujiao4 天前
从 0 到 1 搭建完整 Python 语言 Web UI自动化测试学习系列 17--测试框架Pytest基础 1--介绍使用
python·学习·测试工具·pytest
卓码软件测评5 天前
第三方软件测评机构:【Locust的性能测试和负载测试】
测试工具·测试用例·压力测试·可用性测试
程序员杰哥5 天前
Pytest与Unittest测试框架对比
自动化测试·软件测试·python·测试工具·测试用例·excel·pytest