【原文链接】Pytest如何重复执行N次脚本

在有些场景下,希望将自动化脚本重复执行多次,比如想看看自动化脚本的稳定性,还比如想利用功能测试用例直接进行压力测试,即对某一些脚本反复执行从而对应用产生压力等,Pytest框架中可以通过第三方插件pytest-repeat来实现这样的需求。

首先需要执行如下命令安装pytest-repeat插件

pip install pytest-repeat

种使用方式在脚本中直接使用 @pytest.mark.repeat(N) 标记,比如编写脚本如下,即在test_01上使用 @pytest.mark.repeat(3) 标记。

import pytest@pytest.mark.repeat(3)
def test_01():print("in test01...")assert 1==1def test_02():print("in test02...")assert 1==1

执行结果如下,通过打印也可以看出,这里test_01执行了3次。

(demo--ip5ZZo0) D:\demo>pytest
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: repeat-0.9.1
collected 4 itemstest_demo.py ....                                    [100%]==================== 4 passed in 0.03s ====================(demo--ip5ZZo0) D:\demo>

在实际应用中,这种用法不是很多,因为在通常情况下,脚本是不需要执行多次的,因此通过在执行脚本的命令行中指定重复执行往往是用的最多的。比如这里将测试脚本修改为如下:

def test_01():print("in test01...")assert 1==1def test_02():print("in test02...")assert 1==1

通过在命令行中使用 –count=N 来执行脚本重复执行的次数,比如如下,此时将test_01和test_02均执行了3次。

(demo--ip5ZZo0) D:\demo>pytest -s --count=3
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: repeat-0.9.1
collected 6 itemstest_demo.py in test01...
.in test01...
.in test01...
.in test02...
.in test02...
.in test02...
.==================== 6 passed in 0.03s ====================(demo--ip5ZZo0) D:\demo>

从执行结果中可以看出,执行的过程是将test_01连续执行了3次,然后又将test_02执行了3次,那么这就有一个问题,能不能将test_01和test_02执行完一次,再反复执行test_01和test_02呢,答案是肯定的,这就需要另外一个参数 –repeat-scope 出场了。–repeat-scope参数支持可选的参数有 function、class、module、session,在前面没有指定 –repeat-scope的情况下即默认情况下跟 -repeat-scope=function是一致的,如下即为 –repeat-scope=function的执行结果:

(demo--ip5ZZo0) D:\demo>pytest -s --count=3 --repeat-scope=function
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: repeat-0.9.1
collected 6 itemstest_demo.py in test01...
.in test01...
.in test01...
.in test02...
.in test02...
.in test02...
.==================== 6 passed in 0.03s ====================(demo--ip5ZZo0) D:\demo>

通过指定 –repeat-scope=module 即可做到test_01和test_02作为一个整体循环执行多次,执行结果如下:

(demo--ip5ZZo0) D:\demo>pytest -s --count=3 --repeat-scope=module
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: repeat-0.9.1
collected 6 itemstest_demo.py in test01...
.in test02...
.in test01...
.in test02...
.in test01...
.in test02...
.==================== 6 passed in 0.02s ====================(demo--ip5ZZo0) D:\demo>

下面将测试脚本使用测试类组织,如下:

class TestDemo1():def test_01(self):print("in test01...")assert 1==1def test_02(self):print("in test02...")assert 1==1class TestDemo2():def test_03(self):print("in test03...")assert 1==1def test_04(self):print("in test04...")assert 1==1

接下来通过指定 --repeat-scope=class 来指定把测试类作为重复执行的整体,执行结果如下:

(demo--ip5ZZo0) D:\demo>pytest -s --count=3 --repeat-scope=class
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: repeat-0.9.1
collected 12 itemstest_demo.py in test01...
.in test02...
.in test01...
.in test02...
.in test01...
.in test02...
.in test03...
.in test04...
.in test03...
.in test04...
.in test03...
.in test04...
.=================== 12 passed in 0.16s ====================(demo--ip5ZZo0) D:\demo>

下面可以看下当 –repeat-scope=session 的时候的执行结果,首先编写测试文件test_demo.py和test_demo2.py两个文件,其中test_demo.py文件中的测试代码如下:

class TestDemo1():def test_01(self):print("in test01...")assert 1==1def test_02(self):print("in test02...")assert 1==1

test_demo2.py文件中的测试代码如下:

class TestDemo2():def test_03(self):print("in test03...")assert 1==1def test_04(self):print("in test04...")assert 1==1

通过指定 –repeat-scope=session 的执行结果如下,即将所有脚本作为一个整体进行重复执行了3次。

(demo--ip5ZZo0) D:\demo>pytest -s --count=3 --repeat-scope=session
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
plugins: repeat-0.9.1
collected 12 itemstest_demo.py in test01...
.in test02...
.
test_demo2.py in test03...
.in test04...
.
test_demo.py in test01...
.in test02...
.
test_demo2.py in test03...
.in test04...
.
test_demo.py in test01...
.in test02...
.
test_demo2.py in test03...
.in test04...
.=================== 12 passed in 2.38s ====================(demo--ip5ZZo0) D:\demo>

Pytest如何重复执行N次脚本相关推荐

  1. linux使用flock文件锁解决脚本周期内未执行完重复执行

    linux使用flock文件锁解决脚本周期内未执行完重复执行 关于flock flock 是对于整个文件的建议性锁.也就是说,如果一个进程在一个文件(inode)上放了锁,那么其它进程是可以知道的.( ...

  2. Pytest如何并发执行自动化脚本

    [原文链接]Pytest如何并发执行自动化脚本 当自动化脚本数量非常多的时候,全量脚本的执行耗时很长,此时就希望自动化脚本能够并发执行,第三方插件pytest-xdist 能很好的支持此功能,pyte ...

  3. 为什么.bat脚本不断重复执行同一命令

    为什么.bat脚本不断重复执行同一命令 如果自己写的.bat脚本文件的名称与系统已有的cmd命令重复,会导致该命令在屏幕不断重复显示,得不到脚本想要的结果. 解决方法: 修改.bat脚本文件名称,保证 ...

  4. shell脚本【符号6】“!”重复执行先前命令/非/内部变量

    这一篇博客会介绍"!"符号在不同情境下的使用 1.重复执行先前命令 2.逻辑非 3.作为shell内部变量 4.变量的间接引用 一.重复执行先前命令: ! 在bash中,为了重复执 ...

  5. debian 重复执行sh_debian 脚本启动方式

    同所有的Unix一样,Debian启动时要执行init程序.init的配置文件(/etc/inittab)中指定的第一个执行脚本应该是/etc/init.d/rcS.该脚本执行/etc/rcS.d/目 ...

  6. [Unity][FlowCanvas] 被重复执行的节点,只要其中一次执行出现报错,该报错就会在脚本中一直显示

    我做了一个取 list 的 item 的事件,我看到这个 get item 有报错,但是它还是能传出正确的值,就蒙了 实际上是这个节点重复执行了,第一次出错了,第二次没出错,取到了值 我以为我自己是只 ...

  7. mysql常用增删改脚本 DBA可重复执行.sql文件

    ---------------------------------------------------------------------------------------------------- ...

  8. Oracle数据库可重复执行脚本整理方法

    使用说明 此模板适用于已经建好的数据库和表想导出重复执行的情况 导出的时候建议不要使用Navicat工具,这样会导出很多无用的代码 此模板包括序列.表结构.触发器.注释 使用时替换标明eam_role ...

  9. python+appium自动化测试-重复执行测试用例

    在功能测试过程中,经常会遇到一些偶然出现的Bug,需要通过重复执行用例来复现问题,那么,在自动化测试的过程中,对于一些偶然出现的Bug,也可以针对单个用例,或者针对某个模块的用例重复执行多次来复现. ...

最新文章

  1. 这是一篇工程师对产品经理的吐槽
  2. 【题解】 hdu2955 Robberies
  3. TCL脚本语言基础介绍
  4. C#——扩展.NET Framework基本类型的功能DEMO
  5. 11个鲜为人知的实用Linux命令 - Part 2
  6. 使用命令对象代替switch语句的写法示例
  7. mysql current_MySQL中CURRENT_TIMESTAMP数据类型详解
  8. mysql 大小写问题
  9. jquery之统计数字parseFloat
  10. ElasticSearch基础之批量操作(mget+mbulk)
  11. 运气真不错:3月取到的TeaVM恰好能够运行,之前之后都有问题
  12. 多个中间件_小T说:消息中间件,为什么用RabbitMQ及支持的场景
  13. 方剂学(综合练习)题库【2】
  14. 软件开发中什么是CI/CD
  15. 南京信息工程大学计算机考研怎么样,南京信息工程大学就业怎么样,考研好不好?...
  16. 模板有函数模板和类模板,这个在上学期的java课里面就学了,C++应该是一样的。
  17. 条件随机场CRF的理解
  18. 在Matlab中绘制系统的根轨迹图
  19. C++实验题8 数组使用(bushi)
  20. linux哪个系统好

热门文章

  1. biobakery流程之有参宏基因组分析
  2. MySQL日志(undo log 和 redo log 实现事务的原子性/持久性/一致性)
  3. 【2023最新】超详细图文保姆级教程:App开发新手入门(1)
  4. E:无法定位软件包 zlib-devel
  5. python kfold交叉验证_KFold交叉验证
  6. 【VRP问题】基于遗传算法求解容量和距离约束的车辆路径规划问题CDVRP附matlab代码
  7. Java EE(进阶版)
  8. 前端React项目中实现萤石云ezuikit摄像头的播放与控制
  9. 点击箭头向下 向右切换的方法
  10. 【机器学习基础】支持向量回归