测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----repeat 插件使用方法
收藏本文
作者:redrose2100 类别: 日期:2022-05-15 05:41:20 阅读:1012 次 消耗积分:0 分
### (1)安装pytest-repeat插件 ```bash pip install pytest-repeat ``` ### (2)使用 pytest -s –count=3 命令可以重复执行,不管结果成功还是失败 test_example.py中编写如下代码 ```python def test_01(): assert 1==1 def test_02(): assert 1==2 ``` 重复执行的结果: ```bash (PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s --count=3 ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo plugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0 collected 6 items test_example.py ...FFF =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_02[1-3] _____________________________________________________________________________ def test_02(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError _____________________________________________________________________________ test_02[2-3] _____________________________________________________________________________ def test_02(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError _____________________________________________________________________________ test_02[3-3] _____________________________________________________________________________ def test_02(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_example.py::test_02[1-3] - assert 1 == 2 FAILED test_example.py::test_02[2-3] - assert 1 == 2 FAILED test_example.py::test_02[3-3] - assert 1 == 2 ===================================================================== 3 failed, 3 passed in 0.24s ====================================================================== ``` ### (3)使用 pytest -s –count=3 -x 命令可以重复执行,直到失败位置,如果执行满三次了还没有失败,也不再继续了 test_example.py中编写如下代码: ```python def test_01(): assert 1==1 def test_02(): assert 1==2 ``` 使用如下命令执行:test_02因为第一次就失败了,所以不再继续执行了 ```bash pytest -s --count=3 -x ``` 运行结果如下: ```bash PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s --count=3 -x ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo plugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0 collected 6 items test_example.py ...F =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_02[1-3] _____________________________________________________________________________ def test_02(): > assert 1==2 E assert 1 == 2 test_example.py:6: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_example.py::test_02[1-3] - assert 1 == 2 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ===================================================================== 1 failed, 3 passed in 0.18s ====================================================================== ``` ### (4)使用装饰器标记重复执行的次数 在test_example.py中编写如下代码: ```python import pytest def test_01(): assert 1==1 @pytest.mark.repeat(3) def test_02(): assert 1==2 ``` 此时执行只需要使用 pytest -s即可,运行结果如下: ```bash (PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo plugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0 collected 4 items test_example.py .FFF =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_02[1-3] _____________________________________________________________________________ @pytest.mark.repeat(3) def test_02(): > assert 1==2 E assert 1 == 2 test_example.py:8: AssertionError _____________________________________________________________________________ test_02[2-3] _____________________________________________________________________________ @pytest.mark.repeat(3) def test_02(): > assert 1==2 E assert 1 == 2 test_example.py:8: AssertionError _____________________________________________________________________________ test_02[3-3] _____________________________________________________________________________ @pytest.mark.repeat(3) def test_02(): > assert 1==2 E assert 1 == 2 test_example.py:8: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_example.py::test_02[1-3] - assert 1 == 2 FAILED test_example.py::test_02[2-3] - assert 1 == 2 FAILED test_example.py::test_02[3-3] - assert 1 == 2 ===================================================================== 3 failed, 1 passed in 0.19s ====================================================================== ``` ### (5)重复执行类,只需要在类上加上装饰器即可 test_example.py 中编写如下代码: ```python import pytest @pytest.mark.repeat(3) class TestExample(): def test_01(self): assert 1==1 def test_02(self): assert 1==2 ``` 使用pytest -s执行结果如下: ```bash (PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo plugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0 collected 6 items test_example.py ...FFF =============================================================================== FAILURES =============================================================================== _______________________________________________________________________ TestExample.test_02[1-3] _______________________________________________________________________ self =
def test_02(self): > assert 1==3 E assert 1 == 3 test_example.py:9: AssertionError _______________________________________________________________________ TestExample.test_02[2-3] _______________________________________________________________________ self =
def test_02(self): > assert 1==3 E assert 1 == 3 test_example.py:9: AssertionError _______________________________________________________________________ TestExample.test_02[3-3] _______________________________________________________________________ self =
def test_02(self): > assert 1==3 E assert 1 == 3 test_example.py:9: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_example.py::TestExample::test_02[1-3] - assert 1 == 3 FAILED test_example.py::TestExample::test_02[2-3] - assert 1 == 3 FAILED test_example.py::TestExample::test_02[3-3] - assert 1 == 3 ===================================================================== 3 failed, 3 passed in 0.23s ====================================================================== ``` ### (6)pytest 命令行可通过 –repeat-scope指定重复执行的单元,可选值有:session,module,class,function * (1) function(默认)范围针对每个用例重复执行,再执行下一个用例 * (2) class 以class为用例集合单位,重复执行class里面的用例,再执行下一个 * (3) module 以模块为单位,重复执行模块里面的用例,再执行下一个 * (4) session 重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等 创建如下结构的文件: ```python test_demo |----test_example.py |----test_example_02.py ``` 在两个py文件中都编写如下代码: ```python import pytest def test_001(): assert 1==1 def test_002(): assert 1==2 class TestExample(): def test_003(self): assert 3==3 def test_004(self): assert 3==4 ``` pytest -s –count=3 –repeat-scope=function 运行的结果如下,每个函数都运行三次后在进行下一个 ```bash (PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s --count=3 --repeat-scope=function ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo plugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0 collected 24 items test_example.py ...FFF...FFF test_example_02.py ...FFF...FFF =============================================================================== FAILURES =============================================================================== ____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_example.py::test_002[1-3] - assert 1 == 2 FAILED test_example.py::test_002[2-3] - assert 1 == 2 FAILED test_example.py::test_002[3-3] - assert 1 == 2 FAILED test_example.py::TestExample::test_004[1-3] - assert 3 == 4 FAILED test_example.py::TestExample::test_004[2-3] - assert 3 == 4 FAILED test_example.py::TestExample::test_004[3-3] - assert 3 == 4 FAILED test_example_02.py::test_002[1-3] - assert 1 == 2 FAILED test_example_02.py::test_002[2-3] - assert 1 == 2 FAILED test_example_02.py::test_002[3-3] - assert 1 == 2 FAILED test_example_02.py::TestExample::test_004[1-3] - assert 3 == 4 FAILED test_example_02.py::TestExample::test_004[2-3] - assert 3 == 4 FAILED test_example_02.py::TestExample::test_004[3-3] - assert 3 == 4 ==================================================================== 12 failed, 12 passed in 0.34s ===================================================================== ``` pytest -s –count=3 –repeat-scope=class 运行结果如下,以类为单位进行重复执行3此,然后执行下一个类 ```bash (PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s --count=3 --repeat-scope=class ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo plugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0 collected 24 items test_example.py .F.F.F.F.F.F test_example_02.py .F.F.F.F.F.F =============================================================================== FAILURES =============================================================================== ____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_example.py::test_002[1-3] - assert 1 == 2 FAILED test_example.py::test_002[2-3] - assert 1 == 2 FAILED test_example.py::test_002[3-3] - assert 1 == 2 FAILED test_example.py::TestExample::test_004[1-3] - assert 3 == 4 FAILED test_example.py::TestExample::test_004[2-3] - assert 3 == 4 FAILED test_example.py::TestExample::test_004[3-3] - assert 3 == 4 FAILED test_example_02.py::test_002[1-3] - assert 1 == 2 FAILED test_example_02.py::test_002[2-3] - assert 1 == 2 FAILED test_example_02.py::test_002[3-3] - assert 1 == 2 FAILED test_example_02.py::TestExample::test_004[1-3] - assert 3 == 4 FAILED test_example_02.py::TestExample::test_004[2-3] - assert 3 == 4 FAILED test_example_02.py::TestExample::test_004[3-3] - assert 3 == 4 ==================================================================== 12 failed, 12 passed in 0.26s ===================================================================== ``` pytest -s –count=3 –repeat-scope=module运行结果如下:以模块为单位进行重复执行 ```bash ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo plugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0 collected 24 items test_example.py .F.F.F.F.F.F test_example_02.py .F.F.F.F.F.F =============================================================================== FAILURES =============================================================================== ____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_example.py::test_002[1-3] - assert 1 == 2 FAILED test_example.py::TestExample::test_004[1-3] - assert 3 == 4 FAILED test_example.py::test_002[2-3] - assert 1 == 2 FAILED test_example.py::TestExample::test_004[2-3] - assert 3 == 4 FAILED test_example.py::test_002[3-3] - assert 1 == 2 FAILED test_example.py::TestExample::test_004[3-3] - assert 3 == 4 FAILED test_example_02.py::test_002[1-3] - assert 1 == 2 FAILED test_example_02.py::TestExample::test_004[1-3] - assert 3 == 4 FAILED test_example_02.py::test_002[2-3] - assert 1 == 2 FAILED test_example_02.py::TestExample::test_004[2-3] - assert 3 == 4 FAILED test_example_02.py::test_002[3-3] - assert 1 == 2 FAILED test_example_02.py::TestExample::test_004[3-3] - assert 3 == 4 ==================================================================== 12 failed, 12 passed in 0.28s ===================================================================== ``` pytest -s –count=3 –repeat-scope=session 运行结果如下,以整个测试过程为单位重复执行3此 ```bash (PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s --count=3 --repeat-scope=session ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo plugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0 collected 24 items test_example.py .F.F test_example_02.py .F.F test_example.py .F.F test_example_02.py .F.F test_example.py .F.F test_example_02.py .F.F =============================================================================== FAILURES =============================================================================== ____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example.py:15: AssertionError ____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002(): > assert 1==2 E assert 1 == 2 test_example_02.py:7: AssertionError ______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________ self =
def test_004(self): > assert 3==4 E assert 3 == 4 test_example_02.py:15: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_example.py::test_002[1-3] - assert 1 == 2 FAILED test_example.py::TestExample::test_004[1-3] - assert 3 == 4 FAILED test_example_02.py::test_002[1-3] - assert 1 == 2 FAILED test_example_02.py::TestExample::test_004[1-3] - assert 3 == 4 FAILED test_example.py::test_002[2-3] - assert 1 == 2 FAILED test_example.py::TestExample::test_004[2-3] - assert 3 == 4 FAILED test_example_02.py::test_002[2-3] - assert 1 == 2 FAILED test_example_02.py::TestExample::test_004[2-3] - assert 3 == 4 FAILED test_example.py::test_002[3-3] - assert 1 == 2 FAILED test_example.py::TestExample::test_004[3-3] - assert 3 == 4 FAILED test_example_02.py::test_002[3-3] - assert 1 == 2 FAILED test_example_02.py::TestExample::test_004[3-3] - assert 3 == 4 ==================================================================== 12 failed, 12 passed in 0.27s ===================================================================== ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/209
上一篇:
Pytest----pytest.ini自定义测试文件、测试用例、测试类命名规则
下一篇:
C语言----基于CLion搭建C语言开发环境
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件