Pytest插件pytest-repeat重复执行

pytest,插件,repeat,重复,执行 · 浏览次数 : 33

小编点评

**方法一:装饰器 @pytest.mark.repeat(次数)** ```python import pytest @pytest.mark.repeat(3) def test_a01(): assert 1==2 ``` **方法二:命令行参数语法 pytest --count=3 test_demo.py** ```python import pytest def test_a01(): assert 1==2 ``` **方法三:结合repeat-scope运行** ```python import pytest @pytest.mark.repeat(3, scope='module') def test_a01(): assert 1==2 ``` **使用说明** 1. 导入 pytest库。 2. 使用 pytest 的 `mark.repeat()`装饰器装饰测试函数或类。 3. 指定要重复的次数。 4. 使用 `--count` 参数指定重复次数。 5. 使用 `--repeat-scope` 参数指定循环范围。 6. 使用 `--repeat-scope=session` 参数指定循环范围为会话。

正文

Pytest插件pytest-repeat重复执行

安装

pip install pytest-repeat

doc

https://pypi.org/project/pytest-repeat/

https://github.com/pytest-dev/pytest-repeat

  • 2020年10月31日最后一次更新
  • 最新版本0.9.1
  • 其他没啥内容,就一些简单的使用方法,也在侧面上说明这个插件是比较简单的

使用方法

第一种用法:装饰器 @pytest.mark.repeat(次数)

  • 示例代码

    import pytest
    @pytest.mark.repeat(3)
    def test_a01():
        assert 1==2
    
    if __name__ == '__main__':
        pytest.main(['-sv',__file__])
    
  • 示例输出

    collecting ... collected 3 items
    
    test_demo.py::test_a01[1-3] FAILED
    test_demo.py::test_a01[2-3] FAILED
    test_demo.py::test_a01[3-3] FAILED
    
    ================================== FAILURES ===================================
    ________________________________ test_a01[1-3] ________________________________
    
        @pytest.mark.repeat(3)
        def test_a01():
    >       assert 1==2
    E       assert 1 == 2
    
    test_demo.py:4: AssertionError
    ________________________________ test_a01[2-3] ________________________________
    
        @pytest.mark.repeat(3)
        def test_a01():
    >       assert 1==2
    E       assert 1 == 2
    
    test_demo.py:4: AssertionError
    ________________________________ test_a01[3-3] ________________________________
    
        @pytest.mark.repeat(3)
        def test_a01():
    >       assert 1==2
    E       assert 1 == 2
    
    test_demo.py:4: AssertionError
    =========================== short test summary info ===========================
    FAILED test_demo.py::test_a01[1-3] - assert 1 == 2
    FAILED test_demo.py::test_a01[2-3] - assert 1 == 2
    FAILED test_demo.py::test_a01[3-3] - assert 1 == 2
    ============================== 3 failed in 0.10s ==============================
    
    
  • 从结果看,重复插件的使用会让你的用例变成多个(这点未必是你想要的,要注意)

第二种用法:命令行参数

  • 语法

    pytest --count=3 test_demo.py
    
  • 示例代码

    import pytest
    def test_a01():
        assert 1==2
    
    if __name__ == '__main__':
        pytest.main(['-sv','--count=5',__file__])
    
  • 输出跟上面第一个用法的是一样的

  • 但装饰器是要装饰在每个测试用例上的,而命令行的做法就是一把梭,全部运行多次。

第三种用法:结合repeat-scope运行

  • 如果我们要对多个测试函数进行重复运行,要么加多个装饰器,要么用命令行参数

  • 但是上述两种方法都是A重复,B重复这样,无法做到AB-AB-AB的模式

  • 如果要实现组合重复运行,那就要用到该插件提供的另外一个参数--repeat-scope

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

    • function(默认)范围针对每个用例重复执行,再执行下一个用例
    • class 以class为用例集合单位,重复执行class里面的用例,再执行下一个
    • module 以模块为单位,重复执行模块里面的用例,再执行下一个
    • session 重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等
  • 示例代码1(不加repeat-scope):A运行2次,B运行2次

    import pytest
    
    def test_a():
        assert 1==2
    
    def test_b():
        assert 1==2
    
    if __name__ == '__main__':
        pytest.main(['-sv','--count=2',__file__])
    #运行结果 AABB
    =========================== short test summary info ===========================
    FAILED test_demo.py::test_a[1-2] - assert 1 == 2
    FAILED test_demo.py::test_a[2-2] - assert 1 == 2
    FAILED test_demo.py::test_b[1-2] - assert 1 == 2
    FAILED test_demo.py::test_b[2-2] - assert 1 == 2
    ============================== 4 failed in 0.11s ==============================
    
  • 示例代码(加repeat-scope):A-B运行2次

    import pytest
    
    def test_a():
        assert 1==2
    
    def test_b():
        assert 1==2
    
    if __name__ == '__main__':
        pytest.main(['-sv','--count=2','--repeat-scope=module',__file__])
     
    
    # ABAB
    =========================== short test summary info ===========================
    FAILED test_demo.py::test_a[1-2] - assert 1 == 2
    FAILED test_demo.py::test_b[1-2] - assert 1 == 2
    FAILED test_demo.py::test_a[2-2] - assert 1 == 2
    FAILED test_demo.py::test_b[2-2] - assert 1 == 2
    ============================== 4 failed in 0.11s ==============================
    
  • 如果--repeat-scope=session在此处的效果是一样的

  • 这个插件的--repeat-scope=并没有同步fixture的scope(多了个package)

说在最后

  • Pytest-repeat.py的部分源码

    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')
    

与Pytest插件pytest-repeat重复执行相似的内容:

Pytest插件pytest-repeat重复执行

Pytest插件pytest-repeat重复执行 安装 pip install pytest-repeat doc https://pypi.org/project/pytest-repeat/ https://github.com/pytest-dev/pytest-repeat 2020年10

Pytest插件pytest-order指定用例顺序

Pytest插件pytest-order指定用例顺序 安装 pip install pytest-order 注意不是pytest-ordering 说起来这里有个故事 关于pytest-ordering和pytest-order https://github.com/ftobia/pytest-o

Pytest插件pytest-assume多重断言

Pytest插件pytest-assume多重断言 背景 import pytest def test_assume1(): assert 1 == 2 print('hello') assert 2 == 3 if __name__ == '__main__': pytest.main(['-sv

Pytest插件pytest-rerunfailures失败重跑

Pytest插件pytest-rerunfailures失败重跑 安装 pip install pytest-rerunfailures doc https://github.com/pytest-dev/pytest-rerunfailures https://pypi.org/project/p

Python中的枚举类enum

0. 本文来历 上一篇文章,我写了Pytest插件pytest-order指定用例顺序 我当时就比较好奇它的顺序和英文的对应关系,肯定是写死的,找了下就发现在源码sorter.py中定义了一个dict如下 orders_map = { "first": 0, "second": 1, "third"

浅谈Pytest中的marker

浅谈Pytest中的marker 没有注册marker 我们写一个简单的测试 # test_demo.py import pytest @pytest.mark.login def test_demo(): assert True 你运行的话会有如下提示 test_demo.py:4: Pytest

浅谈Pytest中的warning处理

浅谈Pytest中的warning处理 没有处理warning 我们写一个简单的测试 import pytest def test_demo(): import warnings warnings.warn('test warn',DeprecationWarning) assert True if

详谈pytest中的xfail

详谈pytest中的xfail 原文链接: https://docs.pytest.org/en/7.2.x/how-to/skipping.html 链接中详细阐述了skip和xfail两种情况 xfail 应该译作expected fail,预期失败(我知道这个用例是因为某些原因会失败的) 有哪

数据驱动测试-从方法探研到最佳实践

作者:刘红妍 导读 在自动化测试实践中,测试数据是制造测试场景的必要条件,本文主要讲述了在沟通自动化框架如何分层,数据如何存储,以及基于单元测试pytest下如何执行。并通过实践案例分享,提供数据驱动测试的具体落地方案。 基本概念 数据驱动测试(DDT)是一种方法,其中在数据源的帮助下重复执行相同顺

pytest7.4版本的一个变更,可能会影响你的项目

# pytest7.4版本的一个变更,可能会影响你的项目 > 本文撰写于 2023.7.10 # 准备工作 - 项目结构如下 ``` D:\Gitee\DemoRepo (17.97MB) +-- testCases (1.03KB) | +-- conftest.py (252b) | +-- p