前言

平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些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也可以设置参数: sessionmoduleclass或者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文档28-重复执行用例(pytest-repeat)的更多相关文章

  1. pytest文档33-Hooks函数获取用例执行结果(pytest_runtest_makereport)

    前言 pytest提供的很多钩子(Hooks)方法方便我们对测试用例框架进行二次开发,可以根据自己的需求进行改造. 先学习下pytest_runtest_makereport这个钩子方法,可以更清晰的 ...

  2. pytest文档58-随机执行测试用例(pytest-random-order)

    前言 通常我们认为每个测试用例都是相互独立的,因此需要保证测试结果不依赖于测试顺序,以不同的顺序运行测试用例,可以得到相同的结果. pytest默认运行用例的顺序是按模块和用例命名的 ASCII 编码 ...

  3. pytest文档40-pytest.ini配置用例查找规则(面试题)

    前言 面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例. pytest.ini 配置文件可以修改用例的匹配规则. pytest命令行参数 cmd打开输入pyt ...

  4. pytest文档27-pytest分布式执行(pytest-xdist)

    前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟.如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候, 我们会用测试人力成本换取时间成本,这个时候多找 ...

  5. pytest文档12-skip跳过用例

    前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试. 常见示例是 ...

  6. pytest文档47-allure报告添加用例失败截图

    前言 使用 selenium 做 web 自动化的时候,很多小伙伴希望用例失败的时候能截图,把异常截图展示到allure报告里面. pytest 有个很好的钩子函数 pytest_runtest_ma ...

  7. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  8. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  9. 重复执行用例(pytest-repeat)

    前言 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来.自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或 ...

随机推荐

  1. 20165333实验三 敏捷开发与XP实践

    实验内容 一.参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装alibaba 插件,解决代码中的规范问题. 在IDE ...

  2. 【LOJ】#2244. 「NOI2014」起床困难综合症

    题解 写水题放松一下心情 二进制有个很好的性质是每一位是独立的,我们按位贪心,先看这一位能不能填1,然后看看如果这一位填0那么运算后最后这一位是不是1,是的话就退出,然后看看这一位如果填1最后是1这一 ...

  3. 跟我一起学WPF(2):WPF控件基础

    WPF控件简介 通过上一篇XAML语言的介绍,我们知道,XAML是一个树形结构,同样,WPF控件作为构成整个XAML树的一部分,也是一个树形结构.我们看一个简单的例子. <Button.Cont ...

  4. gluster学习(二)

    GlusterFS卷管理 一.卷基本选项设置 1. 调整高速缓存大小 gluster volume set test-volume performance.cache-size 256MB 2. 详细 ...

  5. 全文搜索引擎 Elasticsearch (一)

    一.安装 Elastic 需要 Java 8 环境.如果你的机器还没安装 Java,可以参考这篇文章,注意要保证环境变量JAVA_HOME正确设置. 安装完 Java,就可以跟着官方文档安装 Elas ...

  6. Scala入门2(特质与叠加在一起的特质)

    一.介绍 参考http://luchunli.blog.51cto.com/2368057/1705025 我们知道,如果几个类有某些共通的方法或者字段,那么从它们多重继承时,就会出现麻烦.所以Jav ...

  7. modCount干嘛的

    在ArrayList.LinkedList.HashMap等等的内部增删改中我们总能看到modCount的身影,modCount字面意思就是修改次数,但为什么要记录modCount的修改次数呢? 大家 ...

  8. linux/mac下命令行rm回收站--rmtrash

    Linux.mac的命令行下没有回收站功能,很多时候手一抖就把重要文件给 rm -fr * 了,虽然linux下有可能通过lost +found/debugfs找回,但难度也比较大,不能保证一定能够找 ...

  9. React Native性能优化之可取消的异步操作

    前沿 在前端的项目开发中,异步操作是一个不可获取的,从用户的角度来说,异步操作所带来的体验是美妙的,但有时候也会带来一些性能隐患.比如说:有一个异步请求还没有返回结果,但是页面却关闭了,这时由于异步操 ...

  10. MySQL 类型转换

    1.问题描述 在项目中遇到要将Int类型转为Char类型,然后利用转化后的Char类型进行模糊查询. 例:合同编号在数据库中为int类型 8066 用利用 806 模糊查询出合同编号为8066数据记录 ...