pytest文档28-重复执行用例(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=10 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(1) assert start == "yoyo" def test_02(start, open_baidu): print("测试用例test_02") time.sleep(1) 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(1) assert start == "yoyo" def test_07(start, open_baidu): print("测试用例test_02") time.sleep(1) 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.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 rootdir: E:\YOYO\web_conf_py, inifile: plugins: xdist-1.23.2, repeat-0.7.0, metadata-1.7.0, html-1.19.0, forked-0.2 collected 2 items baidu\test_1_baidu.py 打开首页 打开百度页面_session 测试用例test_01 .测试用例test_02 . ========================== 2 passed in 1.03 seconds ===========================
加上参数--count=5,用例会重复执行5次
pytest baidu/test_1_baidu.py -s --count=5
E:\YOYO\web_conf_py>pytest baidu/test_1_baidu.py -s --count=5 ============================= test session starts ============================= platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 rootdir: E:\YOYO\web_conf_py, inifile: plugins: xdist-1.23.2, repeat-0.7.0, metadata-1.7.0, html-1.19.0, forked-0.2 collected 10 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 . ========================== 10 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=5 --repeat-scope=session
E:\YOYO\web_conf_py>pytest baidu/test_1_baidu.py -s --count=5 --repeat-scope=session ============================= test session starts ============================= platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 rootdir: E:\YOYO\web_conf_py, inifile: plugins: xdist-1.23.2, repeat-0.7.0, metadata-1.7.0, html-1.19.0, forked-0.2 collected 10 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 . ========================== 10 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(5) 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.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 rootdir: E:\YOYO\web_conf_py, inifile: plugins: xdist-1.23.2, repeat-0.7.0, metadata-1.7.0, html-1.19.0, forked-0.2 collected 6 items baidu\test_1_baidu.py 打开首页 打开百度页面_session 测试用例test_01 .测试用例test_02 .测试用例test_02 .测试用例test_02 .测试用例test_02 .测试用例test_02 . ========================== 6 passed in 3.05 seconds ===========================
重复测试直到失败
如果您正在尝试诊断间歇性故障,那么一遍又一遍地运行相同的测试直到失败是有用的。您可以将pytest的-x选项与pytest-repeat结合使用,以强制测试运行器在第一次失败时停止。例如:
py.test --count=1000 -x test_file.py
这将尝试运行test_file.py 1000次,但一旦发生故障就会停止
UnitTest样式测试
不幸的是,此插件不支持unittest框架的用例,pytest-repeat无法使用unittest.TestCase测试类。无论如何,这些测试将始终运行一次--count,并显示警告
更多资料参考【官方文档:https://pypi.org/project/pytest-repeat/】
---------------------------------pytest结合selenium自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以关注下我的个人公众号:yoyoketang
- pytest文档27-pytest分布式执行(pytest-xdist)
- Python单元测试框架之pytest---如何执行测试用例
- Python之使用unittest框架和HTMLTestRunner.py文件实现多个测试用例执行与测试报告生成
- pytest框架执行用例例子
- pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)
- 用Python搭建自动化测试框架,我们需要组织用例以及测试执行,这里博主推荐Python的标准库——unittest。 unittest是xUnit系列框架中的一员,如果你了解xUnit的其他成员,那
- 【selenium】python+selenium+unittest,关于每次执行完一个测试用例都关闭浏览器等时间较长的问题之解决方案
- python unittest控制用例的执行顺序
- JavaScript中误用/g导致的正则test()无法正确重复执行的解决方案
- 使用testsuit执行测试用例,选择特定用例执行多次的方法(appium+python)
- selenium之unittest批量执行测试用例、生成html报告
- Elastos获取测试用例列表工具--gtci.py(get testcase info)
- selenium---unittest框架测试用例函数执行顺序 优先级
- 跳过测试用例执行(Maven -DskipTests 与 -Dmaven.test.skip=true)
- Coded UI Test 同时执行多个测试用例不必每次都关闭浏览器
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- python基础===利用unittest进行测试用例执行的几种方式
- 1个练习引发的系列学习之pytest指定测试用例 (二)
- Python-unittest---测试用例批量执行
- 加速你的py.test, pytest-dist