IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> Pytest官方文档深入解读(10)如何重执行失败用例 -> 正文阅读

[开发测试]Pytest官方文档深入解读(10)如何重执行失败用例

目录

正文

只重新执行失败用例

使用 --lf 参数,只执行上次失败的用例
test_demo.py代码如下:

import pytest

@pytest.mark.parametrize("i", range(50))
def test_num(i):
    if i in (17, 25):
        pytest.fail("bad luck")

首先使用pytest执行一遍

$ 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参数执行,结果如下,可以发现,此时只执行了两个测试用例

$ 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代码如下:

import pytest

@pytest.mark.parametrize("i", range(50))
def test_num(i):
    if i in (17, 25):
        pytest.fail("bad luck")

首先使用pytest执行一遍

$ 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参数,先执行失败用例,再执行其他用例

$ 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代码如下:

import pytest

@pytest.mark.parametrize("i", range(50))
def test_num(i):
    assert 1==1

首先执行pytest结果如下:

$ 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参数执行结果如下:

$ 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 参数,则可以设置此时不在执行
执行结果如下:

$ 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代码如下:

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 清理缓存

$ 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”打印了,说明缓存中写入成功了

$ 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

$ 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时,用例失败,下面一步一步演示如何调试

import pytest


@pytest.mark.parametrize("i", range(10))
def test_num(i):
    print(i)
    if i in [1,3,5]:
        pytest.fail("bad luck")

执行测试,如下,

$ 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时,失败,停止下来,因此失败一个,通过一个,此时将代码修复,如下:

import pytest


@pytest.mark.parametrize("i", range(10))
def test_num(i):
    print(i)
    if i in [3,5]:
        pytest.fail("bad luck")

再次执行如下

$ 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个,失败一个,再次修复测试脚本如下:

import pytest


@pytest.mark.parametrize("i", range(10))
def test_num(i):
    print(i)
    if i in [5]:
        pytest.fail("bad luck")

再次执行

$ 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个,失败一个,再次修复脚本如下:

import pytest


@pytest.mark.parametrize("i", range(10))
def test_num(i):
    print(i)
    if i in []:
        pytest.fail("bad luck")

再次执行

$ 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个,执行完毕,从而完成脚本的调试

相关推荐

Pytest官方文档深入解读(1)快速入门

Pytest官方文档深入解读(2)如何执行pytest脚本

Pytest官方文档深入解读(3)如何使用断言

Pytest官方文档深入解读(4)如何使用fixture

Pytest官方文档深入解读(5)如何使用mark

Pytest官方文档深入解读(6)如何使用parametrize参数化

Pytest官方文档深入解读(7)如何使用临时目录和文件

Pytest官方文档深入解读(8)如何使用猴子补丁

Pytest官方文档深入解读(9)如何执行文本测试

Pytest官方文档深入解读(10)如何重执行失败用例

Pytest官方文档深入解读(11)如何管理日志

Pytest官方文档深入解读(12)如何捕获标准输出和标准错误输出

Pytest官方文档深入解读(13)如何捕获告警

Pytest官方文档深入解读(14)如何使用skip和xfail

Pytest官方文档深入解读(15)如何安装和使用插件

Pytest官方文档深入解读(16)如何写插件

Pytest官方文档深入解读(17)如何写hook钩子函数

Pytest官方文档深入解读(18)如何针对已有测试套使用pytest

Pytest官方文档深入解读(19)如何使用pytest执行unittest用例

Pytest官方文档深入解读(20)如何使用pytest执行nose用例

Pytest官方文档深入解读(21)如何使用经典的setup和teardown

Pytest官方文档深入解读(22)如何建立bash命令行自动补全

Pytest官方文档深入解读(23)测试脚本的加载原理

Pytest官方文档深入解读(24)Pytest默认的用例发现规则

Pytest官方文档深入解读(25)修改Pytest默认的用例发现规则

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-12-26 22:30:16  更:2021-12-26 22:31:16 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/18 4:30:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码