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')
    
  • 20
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuxianfeng023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值