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('测试用例第三条')

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

相关推荐
黄昏恋慕黎明3 天前
博客论坛技术迭代
python·pytest
2601_962078234 天前
Python接口自动化流程(pytest)
python·pytest·接口自动化·测试框架·断言
杨女士YRJ4 天前
python+pytest+01
python·pytest
2601_962078235 天前
Python接口自动化框架:pytest
python·pytest·接口自动化·测试框架·restfulapi
2601_962078195 天前
Appium+Python+pytest自动化测试框架详解
自动化测试·python·appium·pytest·移动应用
2601_962284507 天前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
2601_962300817 天前
基于pytest的接口自动化测试:使用requests模块的实战项目与接口关联方法详解
接口自动化测试·测试用例·pytest·接口关联·requests模块
2601_962300817 天前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
11路没有终点7 天前
Pytest 安装、版本与第一个测试用例
测试用例·pytest
11路没有终点9 天前
pytest 参数化与 Mark 标记
pytest