Pytest----repeat插件使用方法(pytest自定义插件)

网友投稿 325 2022-08-23


Pytest----repeat插件使用方法(pytest自定义插件)

(1)安装pytest-repeat插件

(PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pip install pytest-repeatCollecting pytest-repeat Downloading pytest_repeat-0.8.0-py2.py3-none-any.whl (4.2 kB)Requirement already satisfied: pytest>=3.6 in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest-repeat) (5.4.3)Requirement already satisfied: colorama; sys_platform == "win32" in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.4.3)Requirement already satisfied: pluggy<1.0,>=0.12 in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.13.1)Requirement already satisfied: packaging in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest>=3.6->pytest-repeat) (20.4)Requirement already satisfied: more-itertools>=4.0.0 in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest>=3.6->pytest-repeat) (8.4.0)Requirement already satisfied: py>=1.5.0 in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest>=3.6->pytest-repeat) (1.9.0)Requirement already satisfied: attrs>=17.4.0 in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest>=3.6->pytest-repeat) (19.3.0)Requirement already satisfied: atomicwrites>=1.0; sys_platform == "win32" in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest>=3.6->pytest-repeat) (1.4.0)Requirement already satisfied: wcwidth in d:\miniconda\envs\pytestenv\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.2.5)Requirement already satisfied: pyparsing>=2.0.2 in d:\miniconda\envs\pytestenv\lib\site-packages (from packaging->pytest>=3.6->pytest-repeat) (2.4.7)Requirement already satisfied: six in d:\miniconda\envs\pytestenv\lib\site-packages (from packaging->pytest>=3.6->pytest-repeat) (1.15.0)

(2)使用 pytest -s –count=3 命令可以重复执行,不管结果成功还是失败

test_example.py中编写如下代码

def test_01(): assert 1==1def test_02(): assert 1==2

重复执行的结果:

(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.1rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demoplugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0collected 6 items test_example.py ...FFF=============================================================================== FAILURES ===============================================================================_____________________________________________________________________________ test_02[1-3] _____________________________________________________________________________ def test_02():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError_____________________________________________________________________________ test_02[2-3] _____________________________________________________________________________ def test_02():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError_____________________________________________________________________________ test_02[3-3] _____________________________________________________________________________ def test_02():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError======================================================================= short test summary info ========================================================================FAILED test_example.py::test_02[1-3] - assert 1 == 2FAILED test_example.py::test_02[2-3] - assert 1 == 2FAILED 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中编写如下代码:

def test_01(): assert 1==1def test_02(): assert 1==2

使用如下命令执行:test_02因为第一次就失败了,所以不再继续执行了

pytest -s --count=3

运行结果如下:

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.1rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demoplugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0collected 6 items test_example.py ...F=============================================================================== FAILURES ===============================================================================_____________________________________________________________________________ test_02[1-3] _____________________________________________________________________________ def test_02():> assert 1==2E assert 1 == 2test_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中编写如下代码:

import pytestdef test_01(): assert 1==1@pytest.mark.repeat(3)def test_02(): assert 1==2

此时执行只需要使用 pytest -s即可,运行结果如下:

(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.1rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demoplugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0collected 4 items test_example.py .FFF=============================================================================== FAILURES ===============================================================================_____________________________________________________________________________ test_02[1-3] _____________________________________________________________________________ @pytest.mark.repeat(3) def test_02():> assert 1==2E assert 1 == 2test_example.py:8: AssertionError_____________________________________________________________________________ test_02[2-3] _____________________________________________________________________________ @pytest.mark.repeat(3) def test_02():> assert 1==2E assert 1 == 2test_example.py:8: AssertionError_____________________________________________________________________________ test_02[3-3] _____________________________________________________________________________ @pytest.mark.repeat(3) def test_02():> assert 1==2E assert 1 == 2test_example.py:8: AssertionError======================================================================= short test summary info ========================================================================FAILED test_example.py::test_02[1-3] - assert 1 == 2FAILED test_example.py::test_02[2-3] - assert 1 == 2FAILED test_example.py::test_02[3-3] - assert 1 == 2===================================================================== 3 failed, 1 passed in 0.19s ======================================================================

(5)重复执行类,只需要在类上加上装饰器即可

test_example.py 中编写如下代码:

import pytest@pytest.mark.repeat(3)class TestExample(): def test_01(self): assert 1==1 def test_02(self): assert 1==2

使用pytest -s执行结果如下:

(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.1rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demoplugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0collected 6 items test_example.py ...FFF=============================================================================== FAILURES ===============================================================================_______________________________________________________________________ TestExample.test_02[1-3] _______________________________________________________________________self = def test_02(self):> assert 1==3E assert 1 == 3test_example.py:9: AssertionError_______________________________________________________________________ TestExample.test_02[2-3] _______________________________________________________________________self = def test_02(self):> assert 1==3E assert 1 == 3test_example.py:9: AssertionError_______________________________________________________________________ TestExample.test_02[3-3] _______________________________________________________________________self = def test_02(self):> assert 1==3E assert 1 == 3test_example.py:9: AssertionError======================================================================= short test summary info ========================================================================FAILED test_example.py::TestExample::test_02[1-3] - assert 1 == 3FAILED test_example.py::TestExample::test_02[2-3] - assert 1 == 3FAILED 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 重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等

创建如下结构的文件:

test_demo |----test_example.py |----test_example_02.py

在两个py文件中都编写如下代码:

import pytestdef test_001(): assert 1==1def test_002(): assert 1==2class TestExample(): def test_003(self): assert 3==3 def test_004(self): assert 3==4

pytest -s –count=3 –repeat-scope=function 运行的结果如下,每个函数都运行三次后在进行下一个

(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.1rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demoplugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0collected 24 items test_example.py ...FFF...FFFtest_example_02.py ...FFF...FFF=============================================================================== FAILURES ===============================================================================____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError======================================================================= short test summary info ========================================================================FAILED test_example.py::test_002[1-3] - assert 1 == 2FAILED test_example.py::test_002[2-3] - assert 1 == 2FAILED test_example.py::test_002[3-3] - assert 1 == 2FAILED test_example.py::TestExample::test_004[1-3] - assert 3 == 4FAILED test_example.py::TestExample::test_004[2-3] - assert 3 == 4FAILED test_example.py::TestExample::test_004[3-3] - assert 3 == 4FAILED test_example_02.py::test_002[1-3] - assert 1 == 2FAILED test_example_02.py::test_002[2-3] - assert 1 == 2FAILED test_example_02.py::test_002[3-3] - assert 1 == 2FAILED test_example_02.py::TestExample::test_004[1-3] - assert 3 == 4FAILED test_example_02.py::TestExample::test_004[2-3] - assert 3 == 4FAILED 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此,然后执行下一个类

(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.1rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demoplugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0collected 24 items test_example.py .F.F.F.F.F.Ftest_example_02.py .F.F.F.F.F.F=============================================================================== FAILURES ===============================================================================____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError======================================================================= short test summary info ========================================================================FAILED test_example.py::test_002[1-3] - assert 1 == 2FAILED test_example.py::test_002[2-3] - assert 1 == 2FAILED test_example.py::test_002[3-3] - assert 1 == 2FAILED test_example.py::TestExample::test_004[1-3] - assert 3 == 4FAILED test_example.py::TestExample::test_004[2-3] - assert 3 == 4FAILED test_example.py::TestExample::test_004[3-3] - assert 3 == 4FAILED test_example_02.py::test_002[1-3] - assert 1 == 2FAILED test_example_02.py::test_002[2-3] - assert 1 == 2FAILED test_example_02.py::test_002[3-3] - assert 1 == 2FAILED test_example_02.py::TestExample::test_004[1-3] - assert 3 == 4FAILED test_example_02.py::TestExample::test_004[2-3] - assert 3 == 4FAILED 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运行结果如下:以模块为单位进行重复执行

========================================================================= test session starts ==========================================================================platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demoplugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0collected 24 items test_example.py .F.F.F.F.F.Ftest_example_02.py .F.F.F.F.F.F=============================================================================== FAILURES ===============================================================================____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError======================================================================= short test summary info ========================================================================FAILED test_example.py::test_002[1-3] - assert 1 == 2FAILED test_example.py::TestExample::test_004[1-3] - assert 3 == 4FAILED test_example.py::test_002[2-3] - assert 1 == 2FAILED test_example.py::TestExample::test_004[2-3] - assert 3 == 4FAILED test_example.py::test_002[3-3] - assert 1 == 2FAILED test_example.py::TestExample::test_004[3-3] - assert 3 == 4FAILED test_example_02.py::test_002[1-3] - assert 1 == 2FAILED test_example_02.py::TestExample::test_004[1-3] - assert 3 == 4FAILED test_example_02.py::test_002[2-3] - assert 1 == 2FAILED test_example_02.py::TestExample::test_004[2-3] - assert 3 == 4FAILED test_example_02.py::test_002[3-3] - assert 1 == 2FAILED 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此

(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.1rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demoplugins: html-2.1.1, metadata-1.10.0, repeat-0.8.0collected 24 items test_example.py .F.Ftest_example_02.py .F.Ftest_example.py .F.Ftest_example_02.py .F.Ftest_example.py .F.Ftest_example_02.py .F.F=============================================================================== FAILURES ===============================================================================____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError____________________________________________________________________________ test_002[1-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError______________________________________________________________________ TestExample.test_004[1-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError____________________________________________________________________________ test_002[2-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError______________________________________________________________________ TestExample.test_004[2-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example.py:7: AssertionError______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example.py:15: AssertionError____________________________________________________________________________ test_002[3-3] _____________________________________________________________________________ def test_002():> assert 1==2E assert 1 == 2test_example_02.py:7: AssertionError______________________________________________________________________ TestExample.test_004[3-3] _______________________________________________________________________self = def test_004(self):> assert 3==4E assert 3 == 4test_example_02.py:15: AssertionError======================================================================= short test summary info ========================================================================FAILED test_example.py::test_002[1-3] - assert 1 == 2FAILED test_example.py::TestExample::test_004[1-3] - assert 3 == 4FAILED test_example_02.py::test_002[1-3] - assert 1 == 2FAILED test_example_02.py::TestExample::test_004[1-3] - assert 3 == 4FAILED test_example.py::test_002[2-3] - assert 1 == 2FAILED test_example.py::TestExample::test_004[2-3] - assert 3 == 4FAILED test_example_02.py::test_002[2-3] - assert 1 == 2FAILED test_example_02.py::TestExample::test_004[2-3] - assert 3 == 4FAILED test_example.py::test_002[3-3] - assert 1 == 2FAILED test_example.py::TestExample::test_004[3-3] - assert 3 == 4FAILED test_example_02.py::test_002[3-3] - assert 1 == 2FAILED test_example_02.py::TestExample::test_004[3-3] - assert 3 == 4==================================================================== 12 failed, 12 passed in 0.27s =====================================================================


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Feign远程调用Multipartfile参数处理
下一篇:数据结构与算法Java版-逆序对问题(java数组逆序输出)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~