测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----如何随机执行用例
收藏本文
作者:redrose2100 类别: 日期:2022-09-28 02:50:45 阅读:795 次 消耗积分:0 分
[TOC] ![](https://redrose2100.oss-cn-hangzhou.aliyuncs.com/img/7cd47362-951c-11ee-986a-0242ac110004.png) 在编写自动化脚本时,一般都要求遵循用例之间是独立的,期望在最终连跑验证的时候能随机执行,因为随机执行更能模拟手工测试的场景。Pytest框架通过提供第三方插件pytest-random-order。 首先需要执行如下命令安装第三方插件 ```bash pip install pytest-random-order ``` 然后编写如下三个用例的测试脚本。 ```python def test_01(): print("in test01...") assert 1==1 def test_02(): print("in test02...") assert 1==1 def test_03(): print("in test03...") assert 1==1 ``` 然后使用 –radom-order 参数即可实现自动化脚本的随机执行,如下,通过打印结果可以发现先执行了test_03,然后执行了test_02,最后执行了test_01 ```bash (demo--ip5ZZo0) D:\demo>pytest -s --random-order =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Using --random-order-bucket=module Using --random-order-seed=937307 rootdir: D:\demo plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 3 items test_demo.py in test03... .in test02... .in test01... . ==================== 3 passed in 2.26s ==================== (demo--ip5ZZo0) D:\demo> ``` 此外,pytest-random-order插件还支持设置一定范围内随机,通过 –random-order-bucket 参数设置范围,可设置的范围有module和class,当然还有其他选项,但是其他选项基本用不着,因此这里只介绍module和class两个选项。为了更好的演示随机性,下面重新编写自动化脚本如下,即一个测试文件中有类外的测试函数,有两个测试类,每个测试类中有三个测试方法。 ```pyhton def test_01(): print("in test01...") assert 1==1 def test_02(): print("in test02...") assert 1==1 def test_03(): print("in test03...") assert 1==1 class TestDemo1(): def test_04(self): print("in test04...") assert 1==1 def test_05(self): print("in test05...") assert 1==1 def test_06(self): print("in test06...") assert 1==1 class TestDemo2(): def test_07(self): print("in test07...") assert 1==1 def test_08(self): print("in test08...") assert 1==1 def test_09(self): print("in test09...") assert 1==1 ``` 首先使用 –random-order 参数,即不指定随机范围的时候执行,执行结果如下,可以发现此时是所有用例都随机执行的。 ```bash (demo--ip5ZZo0) D:\demo>pytest -s --random-order =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Using --random-order-bucket=module Using --random-order-seed=982647 rootdir: D:\demo plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 9 items test_demo.py in test05... .in test04... .in test02... .in test03... .in test07... .in test06... .in test09... .in test01... .in test08... . ==================== 9 passed in 0.03s ==================== (demo--ip5ZZo0) D:\demo> ``` 通过 –random-order-bucket=class 参数则可以指定每个类中的测试方法随机顺序执行,执行结果如下,此时可以做到一个类中的方法随机,这种执行顺序对测试类中有setup和teardown的时候是非常有益的。 ```bash (demo--ip5ZZo0) D:\demo>pytest -s --random-order-bucket=class =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Using --random-order-bucket=class Using --random-order-seed=186060 rootdir: D:\demo plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 9 items test_demo.py in test01... .in test02... .in test03... .in test07... .in test08... .in test09... .in test05... .in test04... .in test06... . ==================== 9 passed in 0.04s ==================== (demo--ip5ZZo0) D:\demo> ``` 如下结果则设置模块内随机,这里因为只有一个测试文件,因此测试文件内所有用例随机执行了。当存在多个测试文件时,则每个测试文件内随机执行。 ```bash (demo--ip5ZZo0) D:\demo>pytest -s --random-order-bucket=module =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Using --random-order-bucket=module Using --random-order-seed=91396 rootdir: D:\demo plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 9 items test_demo.py in test01... .in test08... .in test09... .in test05... .in test04... .in test07... .in test03... .in test02... .in test06... . ==================== 9 passed in 0.04s ==================== (demo--ip5ZZo0) D:\demo> ``` 在随机执行脚本的时候也有新的问题,比如第一次随机执行脚本,中间有脚本失败了,但是再重新随机执行一次时就又不会出现失败了,即某一些用例再一定的执行顺序下才会出现,那么这个时候定位问题是很难的,pytest-random-order插件另外提供了一种方式,即通过使用 –random-order-seed=N 的参数,当第二次再次执行指定同样的N值时,执行的顺序和前一次保持一致,比如第一执行指定N为10000,执行结果如下: ```bash (demo--ip5ZZo0) D:\demo>pytest -s --random-order-seed=10000 =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Using --random-order-bucket=module Using --random-order-seed=10000 rootdir: D:\demo plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 9 items test_demo.py in test06... .in test08... .in test04... .in test05... .in test07... .in test03... .in test09... .in test02... .in test01... . ==================== 9 passed in 0.04s ==================== (demo--ip5ZZo0) D:\demo> ``` 当再次使用 pytest -s --random-order-seed=10000 命令执行时,执行结果如下,可以发现此时的执行顺序和上一次执行完全一致,这样就很容易复现问题,方便定位问题了。 ```bash (demo--ip5ZZo0) D:\demo>pytest -s --random-order-seed=10000 =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0 Using --random-order-bucket=module Using --random-order-seed=10000 rootdir: D:\demo plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0 collected 9 items test_demo.py in test06... .in test08... .in test04... .in test05... .in test07... .in test03... .in test09... .in test02... .in test01... . ==================== 9 passed in 0.04s ==================== (demo--ip5ZZo0) D:\demo> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/402
上一篇:
BootStrap的布局容器和栅格系统
下一篇:
Pytest----如何只执行尚未提交git代码仓脚本
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件