• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest先执行上次失败然后再执行其他用例

武飞扬头像
redrose2100
帮助1

【原文链接】Pytest如何先执行上次失败然后再执行其他用例

在对自动化脚本连跑测试过程中,往往希望优先执行上次连跑失败的脚本,然后再执行其他脚本,Pytest框架支持这样一种连跑机制,通过 –ff 参数即可。下面首先准备如下三个脚本,即第一个和第三个断言失败,第二个断言成功。

def test_01():
    assert 1==2

def test_02():
    assert 2==2

def test_03():
    assert 1==2

首先执行一次,执行结果如下所示,可以看出此时的执行顺序是按照test_01、test_02、test_03的顺序执行的。

(demo--ip5ZZo0) D:\demo>pytest
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
collected 3 items

test_demo.py F.F                                     [100%]

======================== FAILURES =========================
_________________________ test_01 _________________________

    def test_01():
>       assert 1==2
E       assert 1 == 2

test_demo.py:3: AssertionError
_________________________ test_03 _________________________

    def test_03():
>       assert 1==2
E       assert 1 == 2

test_demo.py:9: AssertionError
================= short test summary info =================
FAILED test_demo.py::test_01 - assert 1 == 2
FAILED test_demo.py::test_03 - assert 1 == 2
=============== 2 failed, 1 passed in 0.18s ===============

(demo--ip5ZZo0) D:\demo>

下面使用pytest –ff 参数再次执行,结果如下,可以看出此时是首先执行test_01和test_03,最后执行了test_02,即首先执行了上次失败的用例,然后才执行其他用例。

(demo--ip5ZZo0) D:\demo>pytest --ff
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\demo
collected 3 items
run-last-failure: rerun previous 2 failures first

test_demo.py FF.                                     [100%]

======================== FAILURES =========================
_________________________ test_01 _________________________

    def test_01():
>       assert 1==2
E       assert 1 == 2

test_demo.py:3: AssertionError
_________________________ test_03 _________________________

    def test_03():
>       assert 1==2
E       assert 1 == 2

test_demo.py:9: AssertionError
================= short test summary info =================
FAILED test_demo.py::test_01 - assert 1 == 2
FAILED test_demo.py::test_03 - assert 1 == 2
=============== 2 failed, 1 passed in 0.05s ===============

(demo--ip5ZZo0) D:\demo>

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbagg
系列文章
更多 icon
同类精品
更多 icon
继续加载