重复执行用例插件pytest-repeat的详细使用【pytest系列 11】

1、前言

  • 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶尔会出现一些bug,对于这种问题我们可以针对此用例反复执行多次,最终复现出问题来
  • 自动化运行用例的时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次

2、环境要求

  • Python2.7、3.4+或PyPy
  • pytest2.8 或者更高版本

3、安装插件

pip install pytest-repeat -i https://pypi.tuna.tsinghua.edu.cn/simple

4、使用场景

场景一:重复执行

重复执行用例2次,使用命令:pytest test_01.py -s --count 2 或者 pytest test_01.py -s --count=2

场景二:重复执行直到失败

  • 如果需要验证偶现问题,可以将pytest的-xpytest-repeat结合,以强制运行程序在第一次失败时停止
    pytest --count 1000 -x test_01.py
    
  • 代码
    def test_01():
        import random
        flag = random.choice([False, True, True, True])
        print(flag)
        assert flag
    
  • 执行命令:pytest --count 100 -x test_01.py -s
  • 结果
    =========================================================================== test session starts ============================================================================
    collected 100 items                                                                                                                                                       
    
    test_01.py True
    .True
    .True
    .True
    .True
    .False
    F
    
    ================================================================================= FAILURES =================================================================================
    _____________________________________________________________________________ test_01[6-1000] ______________________________________________________________________________
    
        def test_01():
            import random
            flag = random.choice([False, True, True, True])
            print(flag)
    >       assert flag
    E       assert False
    
    test_01.py:15: AssertionError
    ========================================================================= short test summary info ==========================================================================
    FAILED test_01.py::test_01[6-1000] - assert False
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ======================================================================= 1 failed, 5 passed in 0.11s ========================================================================
    

场景三:标记某个用例重复执行@pytest.mark.repeat(count)

  • 代码:
    import pytest
    
    @pytest.mark.repeat(10)
    def test_01():
        print("测试用例")
    
  • 执行命令:pytest test_01.py -s
  • 结果:
    =========================================================================== test session starts ============================================================================
    platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
    rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnRepeat
    plugins: metadata-2.0.1, rerunfailures-10.2, html-3.1.1, repeat-0.9.1
    collected 5 items                                                                                                                                                          
    
    test_01.py 测试用例
    .测试用例
    .测试用例
    .测试用例
    .测试用例
    .
    
    ============================================================================ 5 passed in 0.02s =============================================================================
    

场景四:使用--repeat-scope指定重复运行的用例范围(粒度)

  • 作用:类似fixture中的scope
    • function 默认值,对范围内每个用例重复执行,然后再执行下一个用例
    • class 以class为用例集合单位,重复执行class中的用例,再执行下一个class
    • module 以模块为单位,重复执行模块里面的用例,然后再执行下一个模块
    • session 重复执行整个测试会话,即所有测试用例执行一次,然后再执行第二次…
指定class
  • 代码
    class TestRepeat:
        def test_01(self):
            print("我是测试用例test_01")
    
        def test_02(self):
            print("我是测试用例test_02")
    
  • 结果1:pytest -s test_01.py::TestRepeat --count 2
    =========================================================================== test session starts ============================================================================
    platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
    rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnRepeat
    plugins: metadata-2.0.1, rerunfailures-10.2, html-3.1.1, repeat-0.9.1
    collected 4 items                                                                                                                                                          
    
    test_01.py 我是测试用例test_01
    .我是测试用例test_01
    .我是测试用例test_02
    .我是测试用例test_02
    .
    
    ============================================================================ 4 passed in 0.02s =============================================================================
    
  • 结果2:pytest -s test_01.py::TestRepeat --repeat-scope class,可以和结果1进行对比
    =========================================================================== test session starts ============================================================================
    platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
    rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnRepeat
    plugins: metadata-2.0.1, rerunfailures-10.2, html-3.1.1, repeat-0.9.1
    collected 4 items                                                                                                                                                          
    
    test_01.py 我是测试用例test_01
    .我是测试用例test_02
    .我是测试用例test_01
    .我是测试用例test_02
    .
    
    ============================================================================ 4 passed in 0.02s =============================================================================
    
指定module
  • 代码
    def test_repeat():
        print("我是测试用例test_repeat")
    
    
    class TestRepeat:
        def test_01(self):
            print("我是测试用例test_01")
    
        def test_02(self):
            print("我是测试用例test_02")
    
  • 结果:pytest -s test_01.py --repeat-scope module
    =========================================================================== test session starts ============================================================================
    platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
    rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnRepeat
    plugins: metadata-2.0.1, rerunfailures-10.2, html-3.1.1, repeat-0.9.1
    collected 6 items                                                                                                                                                          
    
    test_01.py 我是测试用例test_repeat
    .我是测试用例test_01
    .我是测试用例test_02
    .我是测试用例test_repeat
    .我是测试用例test_01
    .我是测试用例test_02
    .
    
    ============================================================================ 6 passed in 0.02s =============================================================================
    

5、兼容性问题

pytest-repeat不能与unittest.TestCase测试类一起使用。无论–count设置多少,都只会运行一次,并显示警告


参考文章:https://www.cnblogs.com/poloyy/p/12691240.html

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值