• Novice学Pytest(13)重复执行用例插件pytestrepeat的详细使用


    一、前言

    • 平常在做功能测试时,经常会遇到某个模块不稳定,偶现一些bug,或者领导临时安排帮忙复现线上比较难出现的bug,我们一般会反复执行多次,最终复现问题
    • 自动化运行用例时,也会偶现bug,可以针对某个用例,或者针对某个模块的用例重复执行多次
    • 安装插件:cmd or pycharm的terminal输入命令:pip install -U pytest-repeat

    二、重复测试直到失败

    • 如果需要验证偶现问题,可以重复运行相同的测试直到失败,这个插件将很有用
    • 可以将pytest的 -x 选项与pytest-repeat结合使用,当测试脚本第一次失败时,-x代表强制停止

      pytest -- count=1000 -x test_repeat.py

    三、小试牛刀

      (1)通过命令

    1 def test_repeat():
    2     import random
    3     assert random.choice([True,False])

      执行命令:pytest --count 5 -x -s test_repeat.py

      执行结果:

      (2)@pytest.mark.repeat(count)

      如果要在代码中将某些测试用例标记为重复执行多次,可以使用@pytest.mark.repeat(count)

    1 import pytest
    2 
    3 @pytest.mark.repeat(5)
    4 def test_repeat():
    5     print("===重复执行多次===")

      执行结果:

    四、--repeat-scope命令行参数

      (1)作用:可以覆盖默认的测试用例执行顺序,类似fixture的scope参数

    • function:默认,范围针对每个用例重复执行,再执行下一个用例
    • class:以class为用例集合单位,重复执行class里面的用例,再执行下一个
    • module:以模块为单位,重复执行模块里面的用例,再执行下一个
    • session:重复整个测试会话,即所有测试用例的执行一次,然后再执行第二次
       (2)案例一:class
    1 class Testrepeat:
    2     def test_one(self):
    3         print("===执行测试用例1===")
    4     def test_two(self):
    5         print("===执行测试用例2===")
    6 
    7 class Testrepeat2:
    8     def test_three(self):
    9         print("===执行测试用例3===")

      输入命令:pytest --count 2 --repeat-scope class -s test_repeat.py

      执行结果:

      (2)案例二:module

     1 class Testrepeat:
     2     def test_one(self):
     3         print("===执行测试用例1===")
     4     def test_two(self):
     5         print("===执行测试用例2===")
     6 
     7 def test_three():
     8     print("===执行用例3===")
     9 
    10 def test_four():
    11     print("===执行用例4===")

      输入命令:pytest --count 2 --repeat-scope module -s test_repeat.py

      执行结果:

    五、兼容性问题

      pytest-repeat不能与unitteset.TestCase测试类一起使用。无论--count设置多少,这些测试始终只运行一次,并显示警告

    参考链接:https://www.cnblogs.com/poloyy/p/12691240.html

  • 相关阅读:
    Servlet
    MySQL游标
    MySQL数据库的备份和还原
    MySQL安全管理
    MySQL存储过程
    MySQL联结——实现多表查询
    MySQL视图
    MySQL触发器
    asp.net core 读取连接字符串
    form表单提交前进行ajax验证
  • 原文地址:https://www.cnblogs.com/huainanhai/p/15173106.html
Copyright © 2020-2023  润新知