专栏文章 pytest-repeat 重复执行用例插件详解

king · 2021年07月12日 · 最后由 king 回复于 2021年07月21日 · 3002 次阅读

前言

场景:有时候我们需要执行的全部用例或者部分用例想要重复执行多次执行的场景,我们随着这个需求往下看就能得到解决。

Pytest 给我们提供了了一个很好的插件 pytest-repeat,简单添加一些参数就可以实现,下面就让我们完整的学习这个插件相关的功能。

pytest-repeat 详解

用法:

  • 添加参数 --count=num
  • 在需要重复执行的用例上使用装饰器 @pytest.mark.repeat(num)

添加参数方式 --count=num

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

def test_repeat():
    assert True

def test_repeat_01():
    assert True

if __name__ == '__main__':
    pytest.main()

在命令窗口执行 pytest -v --count=2 test_repeat.py 命令,查看结果:
在这里插入图片描述

使用装饰器方式 @pytest.mark.repeat(num)

代码示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

@pytest.mark.repeat(2)
def test_repeat():
    assert True

def test_repeat_01():
    assert True

if __name__ == '__main__':
    pytest.main()

在命令窗口执行 pytest -v test_repeat.py 命令,查看结果:
在这里插入图片描述

问题:如果想要设置重复运行用例的时刻,如何设置?

例如:运行完成一个模块之后再重复执行或者运行完成所有用例之后在重复执行这需求。

查看 repeat 源码,发现一个参数可以配置 --repeat-scope

说明:--repeat-scope 参数只能与 --count=num 配合使用

def pytest_addoption(parser):
    parser.addoption(
        '--count',
        action='store',
        default=1,
        type=int,
        help='Number of times to repeat each test')

    parser.addoption(
        '--repeat-scope',
        action='store',
        default='function',
        type=str,
        choices=('function', 'class', 'module', 'session'),
        help='Scope for repeating tests')

--repeat-scope 的参数有 choices=('function', 'class', 'module', 'session') 默认为function

  • function:每个用例重复运行,全部运行结束后在执行下一个用例
  • class:以类为用例集合单位,重复执行类里的用例,在执行下一个用例
  • module:以模块为单位,重复执行模块里面的用例,在执行下一个
  • session:重复整个测试会话,执行完成测试用例后再执行第二遍

让我们看下 --repeat-scope=session

示例代码:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

def test_repeat():
    assert True

def test_repeat_01():
    assert True

if __name__ == '__main__':
    pytest.main()

在命令窗口执行 pytest -v --count=2 --repeat-scope=session test_repeat.py 命令,查看结果:以会话为单位执行,在进行重复执行
在这里插入图片描述

再看一个 --repeat-scope=class

示例代码:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

class TestRepeat:
    def test_repeat(self):
        assert True

class TestRepeat01:
    def test_repeat01(self):
        assert True

if __name__ == '__main__':
    pytest.main()

在命令窗口执行 pytest -v --count=2 --repeat-scope=class test_repeat.py 命令,查看结果:以类为单位执行,重复执行用例
在这里插入图片描述

再看一个 --repeat-scope=module

示例代码上述一样,在命令窗口执行 pytest -v --count=2 --repeat-scope=module test_repeat.py 命令,查看结果:以模块为单位执行,重复执行用例
在这里插入图片描述

总结

  • 重复执行用例通过添加参数 --count=num 和 使用装饰器方式 @pytest.mark.repeat(num) 方式
  • 添加参数 --count=num 可以和 --repeat-scope=作用域配合使用,控制用例重复执行时机
  • 装饰器方式 @pytest.mark.repeat(num) 方式使用--repeat-scope=作用域 控制重复执行时机无效
  • 当时使用添加参数 --count=num执行时,装饰器的用例重复不生效,以--count=num优先

以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!

共收到 4 条回复 时间 点赞


有没有遇到过写了--repeat-scope=class 、还是将 class 之外的用例执行了的情况嘛?

king #2 · 2021年07月17日 Author
刘小样 回复

--repeat-scope=class这个与执行哪些用例没关系,这个作用域的作用是针对重复执行时的时机,--repeat-scope=class 这个代表执行完成一个类后再重复执行,如果测试用例未使用类的方式,重复执行时,将同一个 py 文件里面的函数方式用例看着一个类

应该再补充个,失败重跑的插件的使用,实际应用更大点 ;pytest-rerunfailures 用例失败后重跑

king #4 · 2021年07月21日 Author
难删亦删 回复

这个 pytest 系列的写了很多篇,还没完全复制到这
Pytest 失败重跑 pytest-rerunfailures 插件详解

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册