Pytest 重复执行用例插件----pytest-repeat

前言

在自动化过程中,想要重复执行一条脚本,查看他的稳定性,如果是在unittest框架中,可能会使用for一直循环这条用例,但是当我们使用pytest框架后,我们就可以通过某些插件来实现这个功能了。今天介绍的这个插件就是重复执行某条用例或者某些用例。

pytest-repeat

pytest-repeat属于pytest中的一个第三方插件,它的作用就是重复执行某条用例或者某些用例。

安装: pip install pytest-repeat

使用方法:

复制代码
# 全部执行
pytest  XXX.py  --count=x  其中X表示执行多少次

# 需要在执行的用例上加入装饰器
@pytest.mark.repeat(count)   

重复执行多条用例

当我们想要重复执行多条用例的时候,可以直接通过执行某个py文件来进行多次执行

复制代码
class Test_01:

    def test_01(self):
        print('测试用例第一条')

    def test_02(self):
        print('测试用例第二条')

    def test_03(self):
        print('测试用例第三条')

通过在cmd中输入命令: pytest -s --count=3 # 3表示执行3遍 ,通过执行结果可以看出来,用例已经重复执行了3遍了。

重复执行单个用例

前面介绍使用方法的时候介绍了,如果想要重复执行单条用例的话,我们可以进行对其用例添加装饰器。

复制代码
import pytest


class Test_01:

    @pytest.mark.repeat(2)
    def test_01(self):
        print('测试用例第一条')

    def test_02(self):
        print('测试用例第二条')

    def test_03(self):
        print('测试用例第三条')

再次通过cmd进行执行,注意:这里不需要在添加次数,因为我们在装饰器中已经添加执行次数,如果添加次数的话,会将其他的用例也会重复执行

当然这里也可以进行对多个用例进行添加多条装饰器,这样的话,就能怼不同的用例执行不同的次数

复制代码
import pytest


class Test_01:

    @pytest.mark.repeat(2)
    def test_01(self):
        print('测试用例第一条')

    @pytest.mark.repeat(3)
    def test_02(self):
        print('测试用例第二条')

    @pytest.mark.repeat(4)
    def test_03(self):
        print('测试用例第三条')

直接进行执行,执行结果设置了重复执行多少条用例,就会执行多少条用例

相关推荐
姚青&9 天前
PyCharm 配置与界面化运行
pytest
姚青&9 天前
Pytest 测试用例断言
测试用例·pytest
姚青&9 天前
Pytest 命名规则
pytest
姚青&9 天前
Pytest 测试用例结构
测试用例·pytest
姚青&9 天前
Pytest 简介、安装与环境准备
pytest
测试渣10 天前
持续集成中的自动化测试框架优化实战指南
python·ci/cd·单元测试·自动化·pytest
小凯1234511 天前
pytest框架-详解(学习pytest框架这一篇就够了)
python·学习·pytest
逻极11 天前
pytest 入门指南:Python 测试框架从零到一(2025 实战版)
开发语言·python·pytest