重复执行用例(pytest-repeat)
前言
平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。
自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次。
pytest-repeat
pytest-repeat是pytest的一个插件,用于重复执行单个用例,或多个测试用例,并指定重复次数,pytest-repeat支持的版本:
- Python 2.7, 3.4+ 或 PyPy
- py.test 2.8或更高
使用pip安装pytest-repeat
pip install pytest-repeat
使用--count命令行选项指定要运行测试用例和测试次数
py.test --count= test_file.py
重复执行--count
运行以下代码,项目结构如下
web_conf_py是项目工程名称
│ conftest.py
│ __init__.py
│
├─baidu
│ │ conftest.py
│ │ test_1_baidu.py
│ │ test_2.py
│ │ __init__.py
│
├─blog
│ │ conftest.py
│ │ test_2_blog.py
│ │ __init__.py
代码参考:
# web_conf_py/conftest.py
import pytest @pytest.fixture(scope="session")
def start():
print("\n打开首页")
return "yoyo" # web_conf_py/baidu/conftest.py
import pytest @pytest.fixture(scope="session")
def open_baidu():
print("打开百度页面_session") # web_conf_py/baidu/test_1_baidu.py
import pytest
import time def test_01(start, open_baidu):
print("测试用例test_01")
time.sleep()
assert start == "yoyo" def test_02(start, open_baidu):
print("测试用例test_02")
time.sleep()
assert start == "yoyo" if __name__ == "__main__":
pytest.main(["-s", "test_1_baidu.py"]) # web_conf_py/baidu/test_2.py
import pytest
import time def test_06(start, open_baidu):
print("测试用例test_01")
time.sleep()
assert start == "yoyo"
def test_07(start, open_baidu):
print("测试用例test_02")
time.sleep()
assert start == "yoyo" if __name__ == "__main__":
pytest.main(["-s", "test_2.py"])
cmd进入到工程目录后,不带--count参数只会执行一次
pytest baidu/test_1_baidu.py -s
E:\YOYO\web_conf_py>pytest baidu/test_1_baidu.py -s
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23., repeat-0.7., metadata-1.7., html-1.19., forked-0.2
collected items baidu\test_1_baidu.py
打开首页
打开百度页面_session
测试用例test_01
.测试用例test_02
. ========================== passed in 1.03 seconds ===========================
加上参数--count=5,用例会重复执行5次
pytest baidu/test_1_baidu.py -s --count=
E:\YOYO\web_conf_py>pytest baidu/test_1_baidu.py -s --count=
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23., repeat-0.7., metadata-1.7., html-1.19., forked-0.2
collected items baidu\test_1_baidu.py
打开首页
打开百度页面_session
测试用例test_01
.测试用例test_01
.测试用例test_01
.测试用例test_01
.测试用例test_01
.测试用例test_02
.测试用例test_02
.测试用例test_02
.测试用例test_02
.测试用例test_02
. ========================== passed in 5.06 seconds ==========================
从运行的用例结果看,是先重复5次test_01,再重复5次test_02,有时候我们希望执行的顺序是test_01,test_02按这样顺序重复五次,接下来就用到一个参数--repeat-scope
--repeat-scope
--repeat-scope类似于pytest fixture的scope参数,--repeat-scope也可以设置参数: session , module,class或者function(默认值)
function(默认)范围针对每个用例重复执行,再执行下一个用例class以class为用例集合单位,重复执行class里面的用例,再执行下一个module以模块为单位,重复执行模块里面的用例,再执行下一个session重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等
使用--repeat-scope=session重复执行整个会话用例
pytest baidu/test_1_baidu.py -s --count= --repeat-scope=session
E:\YOYO\web_conf_py>pytest baidu/test_1_baidu.py -s --count= --repeat-scope=session
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23., repeat-0.7., metadata-1.7., html-1.19., forked-0.2
collected items baidu\test_1_baidu.py
打开首页
打开百度页面_session
测试用例test_01
.测试用例test_02
.测试用例test_01
.测试用例test_02
.测试用例test_01
.测试用例test_02
.测试用例test_01
.测试用例test_02
.测试用例test_01
.测试用例test_02
. ========================== passed in 5.07 seconds ==========================
@pytest.mark.repeat(count)
如果要在代码中标记要重复多次的测试,可以使用@pytest.mark.repeat(count)装饰器
# test_1_baidu.py
import pytest
import time def test_01(start, open_baidu):
print("测试用例test_01")
time.sleep(0.5)
assert start == "yoyo" @pytest.mark.repeat()
def test_02(start, open_baidu):
print("测试用例test_02")
time.sleep(0.5)
assert start == "yoyo" if __name__ == "__main__":
pytest.main(["-s", "test_1_baidu.py"])
这样执行用例时候,就不用带上--count参数,只针对test_02重复执行5次
E:\YOYO\web_conf_py>pytest baidu/test_1_baidu.py -s
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23., repeat-0.7., metadata-1.7., html-1.19., forked-0.2
collected items baidu\test_1_baidu.py
打开首页
打开百度页面_session
测试用例test_01
.测试用例test_02
.测试用例test_02
.测试用例test_02
.测试用例test_02
.测试用例test_02
. ========================== passed in 3.05 seconds ===========================
重复测试直到失败
如果您正在尝试诊断间歇性故障,那么一遍又一遍地运行相同的测试直到失败是有用的。您可以将pytest的-x选项与pytest-repeat结合使用,以强制测试运行器在第一次失败时停止。例如:
py.test --count= -x test_file.py
这将尝试运行test_file.py 1000次,但一旦发生故障就会停止
UnitTest样式测试
不幸的是,此插件不支持unittest框架的用例,pytest-repeat无法使用unittest.TestCase测试类。无论如何,这些测试将始终运行一次--count,并显示警告
更多资料参考【官方文档:https://pypi.org/project/pytest-repeat/】
重复执行用例(pytest-repeat)的更多相关文章
- Pytest系列(13)- 重复执行用例插件之pytest-repeat的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 平常在做功能测试的时候,经常 ...
- shell脚本使用技巧4--读取字符,重复执行
ls | cat -n > out.txt 给输出的信息加行号并导出到out.txt 利用shell生成一个独立的进程 pwd; (cd /bin; ls); pwd; 开启一个子shell,不 ...
- Pytest系列(十三)- 重复执行之pytest-repeat的使用
写在前面 这个插件,可以帮助我们很好的解决自动化测试过程中的一些偶线性bug难以复现的问题,但前提是,当前自动化脚本是独立的,不依赖任何其他脚本.个人觉得还是失败重运行的一种体现,就和TestNG是一 ...
- Pytest(6)重复运行用例pytest-repeat
前言 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来. 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例, ...
- pytest(13)-多线程、多进程执行用例
有些项目的测试用例较多,测试用例时需要分布式执行,缩短运行时间. pytest框架中提供可用于分布式执行测试用例的插件:pytest-parallel.pytest-xdist,接下来我们来学习这两个 ...
- 可重复执行的SQL Script
问题 在工作中偶尔会遇到这样的问题:SQL script重复执行时会报错. 理想的状态下,SQL script跑一遍就够了,是不会重复执行的,但是实际情况往往很复杂. 比如Dev同学在开发时在A环境把 ...
- python接口自动化(二十六)--批量执行用例 discover(详解)
简介 我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到 unittest 里面的 discover 方法来加载用例了.加载用例后,用 unittest 里 ...
- python自动化-unittest批量执行用例(discover)
前言 我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到unittet里面的discover方法来加载用例了. 加载用例后,用unittest里面的Text ...
- testng执行用例失败,再次执行
我们通过重写testng的retry方法和transform方法来实现用例失败重跑的功能. 首先添加两个文件 TestngRetry.java public class TestngRetry imp ...
随机推荐
- Linux工具之netstat
1.简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicas ...
- Powerful array CodeForces - 86D (莫队算法)
An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...
- HDU 多校第四场题解
对于 D 题的原题意,出题人和验题人赛前都没有发现标算存在的问题,导致了许多选手的疑惑和时间的浪费,在此表示真诚的歉意! 预计难度分布: Easy - DJKL, Medium - ABCEG, Ha ...
- Mongodb文档查询
MongoDB 查询数据的语法格式如下: db.collection.find(query, projection) query :可选,使用查询操作符指定查询条件 projection :可选,使用 ...
- Java I/O(一)流和文件类File的概述、FileInputStream和FileInputStream
一.流概述 & InputStream.OutputStream 流包括输入流和输出流,即I/O(Input和Output),具体结构如下: I/O类都被放在java.io包中,所有的输入流类 ...
- Attention机制中权重的计算
Attention mechanism中,给输入序列中对应的每一个Ht分配权重(打分)究竟是如何打分? 输入序列打分,a(s, h) 其中s是输出序列的t-1时刻的隐藏层状态,h是输入的多个状态,
- SQL SERVER 2008 设置字段默认值为当前时间
在某些情况下需要对某条记录添加上时间戳,比如用户注册,需要记录用户的注册时间,在SQL SERVER 2008中可以通过 1. 添加新字段 2. 数据类型设置为smalldatetime 3. 默认值 ...
- Tushare基础调用及处理
创建索引: db.daily.createIndex({code:1,date:1,'index':1}) mongodb查看表有几列: map = function() { for (var key ...
- 28. ClustrixDB 分布式架构/评估模型
本节描述如何在数据库中计算查询.在ClustrixDB中,我们跨节点切片数据,然后将查询发送到数据.这是数据库的基本原则之一,它允许随着添加更多节点而几乎线性地扩展. 有关如何分布数据的概念,请参阅数 ...
- CSS 内边距 padding 属性
CSS padding 属性定义元素边框与元素内容之间的空白区域. ㈠padding(填充) ⑴当元素的 padding(填充)内边距被清除时,所释放的区域将会受到元素背景颜色的填充. ⑵单独使用 p ...