测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----如何重复执行N次脚本
收藏本文
作者:redrose2100 类别: 日期:2022-09-27 07:24:27 阅读:1027 次 消耗积分:0 分
[TOC] ![](https://redrose2100.oss-cn-hangzhou.aliyuncs.com/img/7cd47362-951c-11ee-986a-0242ac110004.png) 在有些场景下,希望将自动化脚本重复执行多次,比如想看看自动化脚本的稳定性,还比如想利用功能测试用例直接进行压力测试,即对某一些脚本反复执行从而对应用产生压力等,Pytest框架中可以通过第三方插件pytest-repeat来实现这样的需求。 首先需要执行如下命令安装pytest-repeat插件 ```bash pip install pytest-repeat ``` 种使用方式在脚本中直接使用 @pytest.mark.repeat(N) 标记,比如编写脚本如下,即在test_01上使用 @pytest.mark.repeat(3) 标记。 ```python import pytest @pytest.mark.repeat(3) def test_01(): print("in test01...") assert 1==1 def test_02(): print("in test02...") assert 1==1 ``` 执行结果如下,通过打印也可以看出,这里test_01执行了3次。 ```bash (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 items test_demo.py .... [100%] ==================== 4 passed in 0.03s ==================== (demo--ip5ZZo0) D:\demo> ``` 在实际应用中,这种用法不是很多,因为在通常情况下,脚本是不需要执行多次的,因此通过在执行脚本的命令行中指定重复执行往往是用的最多的。比如这里将测试脚本修改为如下: ```python def test_01(): print("in test01...") assert 1==1 def test_02(): print("in test02...") assert 1==1 ``` 通过在命令行中使用 –count=N 来执行脚本重复执行的次数,比如如下,此时将test_01和test_02均执行了3次。 ```bash (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 items test_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的执行结果: ```bash (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 items test_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作为一个整体循环执行多次,执行结果如下: ```bash (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 items test_demo.py in test01... .in test02... .in test01... .in test02... .in test01... .in test02... . ==================== 6 passed in 0.02s ==================== (demo--ip5ZZo0) D:\demo> ``` 下面将测试脚本使用测试类组织,如下: ```python class TestDemo1(): def test_01(self): print("in test01...") assert 1==1 def test_02(self): print("in test02...") assert 1==1 class TestDemo2(): def test_03(self): print("in test03...") assert 1==1 def test_04(self): print("in test04...") assert 1==1 ``` 接下来通过指定 --repeat-scope=class 来指定把测试类作为重复执行的整体,执行结果如下: ```bash (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 items test_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文件中的测试代码如下: ```python class TestDemo1(): def test_01(self): print("in test01...") assert 1==1 def test_02(self): print("in test02...") assert 1==1 ``` test_demo2.py文件中的测试代码如下: ```python class TestDemo2(): def test_03(self): print("in test03...") assert 1==1 def test_04(self): print("in test04...") assert 1==1 ``` 通过指定 –repeat-scope=session 的执行结果如下,即将所有脚本作为一个整体进行重复执行了3次。 ```bash (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 items 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... . test_demo.py in test01... .in test02... . test_demo2.py in test03... .in test04... . =================== 12 passed in 2.38s ==================== (demo--ip5ZZo0) D:\demo> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/398
上一篇:
Pytest----如何先执行上次失败然后再执行其他用例
下一篇:
Pytest----如何并发执行自动化脚本
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件