测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----如何重执行失败用例
收藏本文
作者:redrose2100 类别: 日期:2022-05-13 17:35:22 阅读:824 次 消耗积分:0 分
[TOC] ## 一、只重新执行失败用例 使用 --lf 参数,只执行上次失败的用例 test_demo.py代码如下: ```python import pytest @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): pytest.fail("bad luck") ``` 首先使用pytest执行一遍 ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 50 items tests\test_demo.py .................F.......F........................ [100%] =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_num[17] _____________________________________________________________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck tests\test_demo.py:7: Failed _____________________________________________________________________________ test_num[25] _____________________________________________________________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck tests\test_demo.py:7: Failed ======================================================================= short test summary info ======================================================================== FAILED tests/test_demo.py::test_num[17] - Failed: bad luck FAILED tests/test_demo.py::test_num[25] - Failed: bad luck ===================================================================== 2 failed, 48 passed in 0.40s ===================================================================== ``` 然后使用--lf参数执行,结果如下,可以发现,此时只执行了两个测试用例 ```bash $ pytest --lf ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 2 items run-last-failure: rerun previous 2 failures tests\test_demo.py FF [100%] =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_num[17] _____________________________________________________________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck tests\test_demo.py:6: Failed _____________________________________________________________________________ test_num[25] _____________________________________________________________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck tests\test_demo.py:6: Failed ======================================================================= short test summary info ======================================================================== FAILED tests/test_demo.py::test_num[17] - Failed: bad luck FAILED tests/test_demo.py::test_num[25] - Failed: bad luck ========================================================================== 2 failed in 0.33s =========================================================================== ``` ## 二、先执行失败用例然后再执行其他用例 使用 --lf 参数,只执行上次失败的用例 test_demo.py代码如下: ```python import pytest @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): pytest.fail("bad luck") ``` 首先使用pytest执行一遍 ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 50 items tests\test_demo.py .................F.......F........................ [100%] =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_num[17] _____________________________________________________________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck tests\test_demo.py:7: Failed _____________________________________________________________________________ test_num[25] _____________________________________________________________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck tests\test_demo.py:7: Failed ======================================================================= short test summary info ======================================================================== FAILED tests/test_demo.py::test_num[17] - Failed: bad luck FAILED tests/test_demo.py::test_num[25] - Failed: bad luck ===================================================================== 2 failed, 48 passed in 0.40s ===================================================================== ``` 然后使用 --ff参数,先执行失败用例,再执行其他用例 ```bash $ pytest --ff ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 50 items run-last-failure: rerun previous 2 failures first tests\test_demo.py FF................................................ [100%] =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_num[17] _____________________________________________________________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck tests\test_demo.py:6: Failed _____________________________________________________________________________ test_num[25] _____________________________________________________________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck tests\test_demo.py:6: Failed ======================================================================= short test summary info ======================================================================== FAILED tests/test_demo.py::test_num[17] - Failed: bad luck FAILED tests/test_demo.py::test_num[25] - Failed: bad luck ===================================================================== 2 failed, 48 passed in 0.40s ===================================================================== ``` ## 三、当上次无失败用例时--lf参数的效果 当上一次没有用例失败是,当再次使用 --lf时,默认动作是执行全部 test_demo.py代码如下: ```python import pytest @pytest.mark.parametrize("i", range(50)) def test_num(i): assert 1==1 ``` 首先执行pytest结果如下: ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 50 items tests\test_demo.py .................................................. [100%] ========================================================================== 50 passed in 0.30s ========================================================================== ``` 此时使用 --lf参数执行结果如下: ```bash $ pytest --lf ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 50 items run-last-failure: no previously failed tests, not deselecting items. tests\test_demo.py .................................................. [100%] ========================================================================== 50 passed in 0.30s ========================================================================== ``` 当再机上 --last-failed-no-failures none 参数,则可以设置此时不在执行 执行结果如下: ```bash $ pytest --lf --last-failed-no-failures none ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 50 items / 50 deselected run-last-failure: no previously failed tests, deselecting all items. ======================================================================== 50 deselected in 0.23s ======================================================================== ``` ## 四、前后两次执行之间可通过cache传递数据 通过request.config.cache.set 向缓存中写入数据,request.config.cache.get从缓存中获取数据 test_demo.py代码如下: ```python import pytest @pytest.fixture def mydata(request): val = request.config.cache.get("example/value", None) if val is None: print("example/value is None, now begin to set value to example/value") val = 42 request.config.cache.set("example/value", val) return val ``` 第一次执行执行结果如下,这里因为测试过多次,所以加上 --cache-clear 清理缓存 ```bash $ pytest -s --cache-clear ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py example/value is None, now begin to set value to example/value F =============================================================================== FAILURES =============================================================================== ____________________________________________________________________________ test_function _____________________________________________________________________________ mydata = 42 def test_function(mydata): > assert mydata == 23 E assert 42 == 23 test_demo.py:14: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_function - assert 42 == 23 ========================================================================== 1 failed in 0.10s =========================================================================== ``` 再次执行如下,发现此时美哦与“example/value is None, now begin to set value to example/value”打印了,说明缓存中写入成功了 ```bash $ pytest -s ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py F =============================================================================== FAILURES =============================================================================== ____________________________________________________________________________ test_function _____________________________________________________________________________ mydata = 42 def test_function(mydata): > assert mydata == 23 E assert 42 == 23 test_demo.py:14: AssertionError ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_function - assert 42 == 23 ========================================================================== 1 failed in 0.09s =========================================================================== ``` 此时可以通过 pytest --cache-show 查看缓存中的数据,可以发现,确实可以看到example\value的值为42 ```bash $ pytest --cache-show ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 cachedir: D:\src\blog\tests\.pytest_cache ------------------------------------------------------------------------- cache values for '*' ------------------------------------------------------------------------- cache\lastfailed contains: {'test_demo.py::test_function': True} cache\nodeids contains: ['test_demo.py::test_function'] cache\stepwise contains: [] example\value contains: 42 ======================================================================== no tests ran in 0.01s ========================================================================= ``` ## 五、调试用例再次执行从上次失败处执行 可以通过 --sw 参数,即--stepwise,做到调试用例,当遇到失败时,就停下来,然后手工去修复用例,用例修复后,再执行时从上次失败处执行,遇到失败后再次停下来...... test_demo.py代码如下,即执行10个用例,其中,当i为1,3,5时,用例失败,下面一步一步演示如何调试 ```python import pytest @pytest.mark.parametrize("i", range(10)) def test_num(i): print(i) if i in [1,3,5]: pytest.fail("bad luck") ``` 执行测试,如下, ```bash $ pytest --sw ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 10 items stepwise: no previously failed tests, not skipping. test_demo.py .F =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_num[1] ______________________________________________________________________________ i = 1 @pytest.mark.parametrize("i", range(10)) def test_num(i): print(i) if i in [1,3,5]: > pytest.fail("bad luck") E Failed: bad luck test_demo.py:8: Failed ------------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------- 1 ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_num[1] - Failed: bad luck !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: Test failed, continuing from this test next run. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ===================================================================== 1 failed, 1 passed in 0.14s ====================================================================== ``` 当i为0时,成功,当i为1时,失败,停止下来,因此失败一个,通过一个,此时将代码修复,如下: ```python import pytest @pytest.mark.parametrize("i", range(10)) def test_num(i): print(i) if i in [3,5]: pytest.fail("bad luck") ``` 再次执行如下 ```bash $ pytest --sw ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 10 items / 1 deselected / 9 selected stepwise: skipping 1 already passed items. test_demo.py ..F =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_num[3] ______________________________________________________________________________ i = 3 @pytest.mark.parametrize("i", range(10)) def test_num(i): print(i) if i in [3,5]: > pytest.fail("bad luck") E Failed: bad luck test_demo.py:8: Failed ------------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------- 3 ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_num[3] - Failed: bad luck !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: Test failed, continuing from this test next run. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ============================================================== 1 failed, 2 passed, 1 deselected in 0.15s =============================================================== ``` 此时,即i从1开始执行,当i为1,2时成功,当i为3时失败,因此通过2个,失败一个,再次修复测试脚本如下: ```python import pytest @pytest.mark.parametrize("i", range(10)) def test_num(i): print(i) if i in [5]: pytest.fail("bad luck") ``` 再次执行 ```bash $ pytest --sw ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 10 items / 3 deselected / 7 selected stepwise: skipping 3 already passed items. test_demo.py ..F =============================================================================== FAILURES =============================================================================== _____________________________________________________________________________ test_num[5] ______________________________________________________________________________ i = 5 @pytest.mark.parametrize("i", range(10)) def test_num(i): print(i) if i in [5]: > pytest.fail("bad luck") E Failed: bad luck test_demo.py:8: Failed ------------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------- 5 ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_num[5] - Failed: bad luck !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: Test failed, continuing from this test next run. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ============================================================== 1 failed, 2 passed, 3 deselected in 0.15s =============================================================== ``` 此时i从3开始执行,当i为3,4时成功,当i为5时失败,因此通过2个,失败一个,再次修复脚本如下: ```python import pytest @pytest.mark.parametrize("i", range(10)) def test_num(i): print(i) if i in []: pytest.fail("bad luck") ``` 再次执行 ```bash $ pytest --sw ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 10 items / 5 deselected / 5 selected stepwise: skipping 5 already passed items. test_demo.py ..... [100%] =================================================================== 5 passed, 5 deselected in 0.03s ==================================================================== ``` 此时i从5开始执行,当i为5,6,7,8,9时成功,因此成功5个,执行完毕,从而完成脚本的调试
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/188
上一篇:
Pytest----Pytest如何执行文本测试
下一篇:
Pytest----如何正确使用pytest的日志功能
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件