平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。
自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次。
使用pip安装pytest-repeat
pip install pytest-repeatpytest-repeat 基本使用
test_rp.py 示例内容
-  def test_a1():
-  print("a1-------")
-  def test_a2():
-  print("a2------")
命令行带参数--count=3执行,n是重复次数
pytest --count=3 test_rp.py执行结果
-  collected 6 items
-  test_rp.py::test_a1[1-3] PASSED [ 16%]
-  test_rp.py::test_a1[2-3] PASSED [ 33%]
-  test_rp.py::test_a1[3-3] PASSED [ 50%]
-  test_rp.py::test_a2[1-3] PASSED [ 66%]
-  test_rp.py::test_a2[2-3] PASSED [ 83%]
-  test_rp.py::test_a2[3-3] PASSED [100%]
从运行结果可以看出,它是 test_a1 执行3次后,再 test_a2 执行3次。
--repeat-scope参数类似于pytest fixture的scope参数,—repeat-scope也可以设置参数:session , module,class或者function(默认值)
-  function(默认)范围针对每个用例重复执行,再执行下一个用例 
-  class 以class为用例集合单位,重复执行class里面的用例,再执行下一个 
-  module 以模块为单位,重复执行模块里面的用例,再执行下一个 
-  session 重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等 
使用--repeat-scope=module 重复执行整个模块
pytest --count=3 --repeat-scope=module test_rp.pymark 标记单个用例重复执行
-  import pytest
-  def test_a1():
-  print("a1-------")
-  @pytest.mark.repeat(3)
-  def test_a2():
-  print("a2------")
'
运行
运行
在 yaml 用例中重复运行
test_rp.yml文件内容
-  test_a1:
-  print: "a1-------"
-  test_a2:
-  print: "a2------"
按模块(单个yaml文件就得一个模块)重复运行
pytest --count=3 --repeat-scope=module test_rp.yml运行结果跟前面写的py文件用例一样。
在yaml 文件中mark单个用例重复运行
-  test_a1:
-  print: "a1-------"
-  test_a2:
-  mark: repeat(3)
-  print: "a2------"
此时test_a2会被执行3次。
感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取




















