Pytest系列(13)- 重复执行用例插件之pytest-repeat的详细使用

简介: Pytest系列(13)- 重复执行用例插件之pytest-repeat的详细使用

如果你还想从头学起Pytest,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1690628.html

 

前言


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


环境前提


  • Python 2.7、3.4+或PyPy
  • py.test 2.8或更高版本

 

安装插件


pip3 install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

 

快速入门


结合之前讲到的失败重跑、输出html报告插件来敲命令行

两种方式皆可,等号或空格

  • count=2
  • count 2

pytest --html=report.html --self-contained-html  -s --reruns=5 --count=2 10fixture_request.py

 

重复测试直到失败(重点!)


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

py.test --count=1000 -x test_file.py

 

小栗子

def test_example():

   import random

   flag = random.choice([True, False])

   print(flag)

   assert flag

 

执行命令

  pytest -s --count 5 -x 13repeat.py

 

执行结果

image.png


@pytest.mark.repeat(count)


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

@pytest.mark.repeat(5)

def test_repeat():

   print("测试用例执行")

 

执行命令

pytest -s 13repeat.py

 

执行结果

image.png


--repeat-scope


命令行参数

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

  • function:默认,范围针对每个用例重复执行,再执行下一个用例
  • class:以class为用例集合单位,重复执行class里面的用例,再执行下一个
  • module:以模块为单位,重复执行模块里面的用例,再执行下一个
  • session:重复整个测试会话,即所有测试用例的执行一次,然后再执行第二次

 

案例一:class

class Test_repeat:
    def test_repeat3(self):
        print("测试用例执行333")
class Test_repeat2:
    def test_repeat3(self):
        print("测试用例执行444")


执行命令

pytest -s --count=2 --repeat-scope=class 13repeat.py

执行结果

image.png


案例二:module

def test_repeat1():
    print("测试用例执行111")
def test_repeat2():
    print("测试用例执行222")
class Test_repeat:
    def test_repeat3(self):
        print("测试用例执行333")


执行命令

pytest -s --count=2 --repeat-scope=module 13repeat.py


执行结果

image.png


兼容性问题

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


相关文章
|
4月前
|
测试技术 Python
设置pycharm使用pytest执行测试用例时,输出print语句至控制台
设置pycharm使用pytest执行测试用例时,输出print语句至控制台
66 0
|
9月前
|
测试技术 Python
Pytest用例执行的先后顺序
Pytest用例执行的先后顺序
92 0
|
10月前
|
测试技术
30-pytest-重复执行用例-pytest-repeat
30-pytest-重复执行用例-pytest-repeat
30-pytest-重复执行用例-pytest-repeat
|
10月前
|
测试技术
15-pytest-自定义用例执行顺序
15-pytest-自定义用例执行顺序
|
10月前
|
测试技术
16-pytest-skip跳过用例
16-pytest-skip跳过用例
|
10月前
|
测试技术 Python
02-pytest-用例运行规则
02-pytest-用例运行规则
|
测试技术 Python
通过代码实例解析Pytest运行流程
通过代码实例解析Pytest运行流程
149 0
|
12月前
|
测试技术 C++ Python
pytest 执行规则_基本用法_常用插件_常用断言_常用参数
pytest 执行规则_基本用法_常用插件_常用断言_常用参数
|
12月前
|
测试技术 C++
pytest pytest.ini配置 用例分组 用例跳过
pytest pytest.ini配置 用例分组 用例跳过
|
测试技术 Python
Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用
Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用
549 0