Pytest插件pytest-repeat重复执行

安装

pip install pytest-repeat

doc

https://pypi.org/project/pytest-repeat/

https://github.com/pytest-dev/pytest-repeat

  • 2020年10月31日最后一次更新
  • 最新版本0.9.1
  • 其他没啥内容,就一些简单的使用方法,也在侧面上说明这个插件是比较简单的

使用方法

第一种用法:装饰器 @pytest.mark.repeat(次数)

  • 示例代码

    import pytest
    @pytest.mark.repeat(3)
    def test_a01():
    assert 1==2 if __name__ == '__main__':
    pytest.main(['-sv',__file__])
  • 示例输出

    collecting ... collected 3 items
    
    test_demo.py::test_a01[1-3] FAILED
    test_demo.py::test_a01[2-3] FAILED
    test_demo.py::test_a01[3-3] FAILED ================================== FAILURES ===================================
    ________________________________ test_a01[1-3] ________________________________ @pytest.mark.repeat(3)
    def test_a01():
    > assert 1==2
    E assert 1 == 2 test_demo.py:4: AssertionError
    ________________________________ test_a01[2-3] ________________________________ @pytest.mark.repeat(3)
    def test_a01():
    > assert 1==2
    E assert 1 == 2 test_demo.py:4: AssertionError
    ________________________________ test_a01[3-3] ________________________________ @pytest.mark.repeat(3)
    def test_a01():
    > assert 1==2
    E assert 1 == 2 test_demo.py:4: AssertionError
    =========================== short test summary info ===========================
    FAILED test_demo.py::test_a01[1-3] - assert 1 == 2
    FAILED test_demo.py::test_a01[2-3] - assert 1 == 2
    FAILED test_demo.py::test_a01[3-3] - assert 1 == 2
    ============================== 3 failed in 0.10s ==============================
  • 从结果看,重复插件的使用会让你的用例变成多个(这点未必是你想要的,要注意)

第二种用法:命令行参数

  • 语法

    pytest --count=3 test_demo.py
  • 示例代码

    import pytest
    def test_a01():
    assert 1==2 if __name__ == '__main__':
    pytest.main(['-sv','--count=5',__file__])
  • 输出跟上面第一个用法的是一样的

  • 但装饰器是要装饰在每个测试用例上的,而命令行的做法就是一把梭,全部运行多次。

第三种用法:结合repeat-scope运行

  • 如果我们要对多个测试函数进行重复运行,要么加多个装饰器,要么用命令行参数

  • 但是上述两种方法都是A重复,B重复这样,无法做到AB-AB-AB的模式

  • 如果要实现组合重复运行,那就要用到该插件提供的另外一个参数--repeat-scope

  • --repeat-scope类似于pytest fixture的scope参数,--repeat-scope也可以设置参数: sessionmoduleclass或者function(默认值)

    • function(默认)范围针对每个用例重复执行,再执行下一个用例
    • class 以class为用例集合单位,重复执行class里面的用例,再执行下一个
    • module 以模块为单位,重复执行模块里面的用例,再执行下一个
    • session 重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等
  • 示例代码1(不加repeat-scope):A运行2次,B运行2次

    import pytest
    
    def test_a():
    assert 1==2 def test_b():
    assert 1==2 if __name__ == '__main__':
    pytest.main(['-sv','--count=2',__file__])
    #运行结果 AABB
    =========================== short test summary info ===========================
    FAILED test_demo.py::test_a[1-2] - assert 1 == 2
    FAILED test_demo.py::test_a[2-2] - assert 1 == 2
    FAILED test_demo.py::test_b[1-2] - assert 1 == 2
    FAILED test_demo.py::test_b[2-2] - assert 1 == 2
    ============================== 4 failed in 0.11s ==============================
  • 示例代码(加repeat-scope):A-B运行2次

    import pytest
    
    def test_a():
    assert 1==2 def test_b():
    assert 1==2 if __name__ == '__main__':
    pytest.main(['-sv','--count=2','--repeat-scope=module',__file__]) # ABAB
    =========================== short test summary info ===========================
    FAILED test_demo.py::test_a[1-2] - assert 1 == 2
    FAILED test_demo.py::test_b[1-2] - assert 1 == 2
    FAILED test_demo.py::test_a[2-2] - assert 1 == 2
    FAILED test_demo.py::test_b[2-2] - assert 1 == 2
    ============================== 4 failed in 0.11s ==============================
  • 如果--repeat-scope=session在此处的效果是一样的

  • 这个插件的--repeat-scope=并没有同步fixture的scope(多了个package)

说在最后

  • Pytest-repeat.py的部分源码

    def pytest_addoption(parser):
    parser.addoption(
    '--count',
    action='store',
    default=1,
    type=int,
    help='Number of times to repeat each test') parser.addoption(
    '--repeat-scope',
    action='store',
    default='function',
    type=str,
    choices=('function', 'class', 'module', 'session'),
    help='Scope for repeating tests')

Pytest插件pytest-repeat重复执行的更多相关文章

  1. linux命令之 repeat 重复执行命令

    $ vim ~/.bashrc function repeat() { number=$1 shift echo $@ for n in $(seq $number); do $@ done } $ ...

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

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

  3. Pytest系列(13)- 重复执行用例插件之pytest-repeat的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 平常在做功能测试的时候,经常 ...

  4. Pytest系列(十三)- 重复执行之pytest-repeat的使用

    写在前面 这个插件,可以帮助我们很好的解决自动化测试过程中的一些偶线性bug难以复现的问题,但前提是,当前自动化脚本是独立的,不依赖任何其他脚本.个人觉得还是失败重运行的一种体现,就和TestNG是一 ...

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

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

  6. Pytest(6)重复运行用例pytest-repeat

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

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

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

  8. Pytest测试框架(一):pytest安装及用例执行

    PyTest是基于Python的开源测试框架,语法简单易用,有大量的插件,功能非常多.自动检测测试用例,支持参数化,跳过特定用例,失败重试等功能. 安装 pip install -U pytest  ...

  9. Pytest(16)随机执行测试用例pytest-random-order

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

  10. 二、为什么要选用pytest以及 pytest与unittest比较

    为什么要选择pytest,我看中的如下: 写case,不需要像unittest那样,创建测试类,继承unittest.TestCase pytest中的fixture(类似于setUp.tearDow ...

随机推荐

  1. 【炫丽】从0开始做一个WPF+Blazor对话小程序

    大家好,我是沙漠尽头的狼. .NET是免费,跨平台,开源,用于构建所有应用的开发人员平台. 本文演示如何在WPF中使用Blazor开发漂亮的UI,为客户端开发注入新活力. 注 要使WPF支持Blazo ...

  2. 网页嵌入zabbix页面(不同域名)

    先来结论: 方案一:绕过身份验证:https://www.cnblogs.com/JaSonS-toy/p/4939805.html(我不是这样实现,可以自行尝试) 方案二: 1.保证请求的ip与请求 ...

  3. pagehelper使用有误导致sql多了一个limit

    接口测试报告中发现时不时有一个接口报错,但再跑一次又没有这个报错了.报错信息是sql异常,sql中有两个limit.查看后台代码和XXmapper.xml,发现确实只有一个limit.一开始开发以为是 ...

  4. 基于python的数学建模---传染病六个模型

    六个模型的区别 SI-Model import scipy.integrate as spi import numpy as np import matplotlib.pyplot as plt # ...

  5. “XZ”格式文件解压

    1.下载xz 官网:https://tukaani.org/xz/ 例:wget https://nchc.dl.sourceforge.net/project/lzmautils/xz-5.2.6. ...

  6. [论文阅读] 颜色迁移-Linear Monge-Kantorovitch(MKL)

    [论文阅读] 颜色迁移-Linear Monge-Kantorovitch(MKL) 文章: The Linear Monge-Kantorovitch Linear Colour Mapping f ...

  7. 《Java口袋指南》-内容总结

    Java口袋指南   一.语言   1.命名   类名:大驼峰   泛型:E标识集合元素   方法名:小驼峰   变量名:小写   包名:小写或下划线   2.词法元素/token   字符串压缩优化 ...

  8. linux基础第二部分

    一.Linux命令执行过程 先判断是否是别名,如果是直接执行,不是看是否是内部命令 如果是内部命令,直接执行,不是看hash表 hash表中有源文件直接执行,找不到报错 若hash表中不存在去外部规定 ...

  9. 前端(js部分讲解)

    BOM操作 BOM概念 BOM:Browser Object Model 是浏览器对象模型,浏览器对象模型提供了独立与内容的.可以与浏览器窗口进行互动的对象结构,BOM由多个对象构成,其中代表浏览器窗 ...

  10. vue 点击按钮添加一行dom节点

    如图,最近项目需求,点击添加一行dom节点,包含下拉框和input输入框 ,下面展示一下代码 <ul class="sales-menuItem-ul"> <li ...