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

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

相关推荐
头疼的程序员1 天前
allure生成测试报告(搭配Pytest、allure-pytest)
测试工具·pytest
文人sec2 天前
接口自动化测试设计思路--设计实战
python·https·单元测试·自动化·pytest
测试开发Kevin3 天前
从投入产出、效率、上手难易度等角度综合对比 pytest 和 unittest 框架
python·pytest
测试开发Kevin5 天前
以pytest_addoption 为例,讲解pytest框架中钩子函数的应用
python·pytest
川石教育11 天前
Pytest中的fixture装饰器详解
python自动化测试·pytest·pytest自动化测试框架·pytest测试框架·pytest单元测试框架
春风又。11 天前
接口自动化——参数化
python·测试工具·自动化·pytest
XTY0014 天前
mac电脑pytest生成测试报告
pytest
程序员的世界你不懂14 天前
pytest-前后置及fixture运用
pytest
天才测试猿15 天前
基于Pytest接口自动化的requests模块项目实战以及接口关联方法详解
自动化测试·软件测试·python·测试工具·单元测试·测试用例·pytest
HtwHUAT16 天前
五、UI自动化测试05--PyTest框架
经验分享·python·ui·pytest