Novice学Pytest(13)-重复执行用例插件pytest-repeat的详细使用

2022/4/22 23:12:53

本文主要是介绍Novice学Pytest(13)-重复执行用例插件pytest-repeat的详细使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、前言

  • 平常在做功能测试时,经常会遇到某个模块不稳定,偶现一些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



这篇关于Novice学Pytest(13)-重复执行用例插件pytest-repeat的详细使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程