pytest + yaml 框架 -44.支持pytest-repeat插件重复执行用例

前言

平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。
自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次。
备注:从v1.2.4 以后新版本不再公开,新功能内部 VIP 学员可以使用,公开版本仅解决bug, 不提供新功能了。
使用pip安装pytest-repeat

pip install pytest-repeat

pytest-repeat 基本使用

test_rp.py 示例内容


def test_a1():
    print("a1-------")


def test_a2():
    print("a2------")

命令行带参数--count=3执行,n是重复次数

pytest --count=3 test_rp.py

执行结果

collected 6 items                                                                                                        

test_rp.py::test_a1[1-3] PASSED                    [ 16%]
test_rp.py::test_a1[2-3] PASSED                    [ 33%]
test_rp.py::test_a1[3-3] PASSED                    [ 50%]
test_rp.py::test_a2[1-3] PASSED                    [ 66%]
test_rp.py::test_a2[2-3] PASSED                    [ 83%]
test_rp.py::test_a2[3-3] PASSED                    [100%]

从运行结果可以看出,它是 test_a1 执行3次后,再 test_a2 执行3次。

--repeat-scope参数类似于pytest fixture的scope参数,--repeat-scope也可以设置参数: session , module,class或者function(默认值)

  • function(默认)范围针对每个用例重复执行,再执行下一个用例
  • class 以class为用例集合单位,重复执行class里面的用例,再执行下一个
  • module 以模块为单位,重复执行模块里面的用例,再执行下一个
  • session 重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等

使用--repeat-scope=module 重复执行整个模块

pytest --count=3 --repeat-scope=module test_rp.py

mark 标记单个用例重复执行

import pytest


def test_a1():
    print("a1-------")


@pytest.mark.repeat(3)
def test_a2():
    print("a2------")

在 yaml 用例中重复运行

test_rp.yml文件内容

test_a1:
    print: "a1-------"


test_a2:
    print: "a2------"

按模块(单个yaml文件就得一个模块)重复运行

pytest --count=3 --repeat-scope=module test_rp.yml

运行结果跟前面写的py文件用例一样。

在yaml 文件中mark单个用例重复运行

test_a1:
    print: "a1-------"



test_a2:
    mark: repeat(3)
    print: "a2------"

此时test_a2会被执行3次。
网易云完整视频课程https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)

posted @ 2023-06-27 22:56  上海-悠悠  阅读(240)  评论(0编辑  收藏  举报