pytest特性记录:遇到用例执行失败则停止测试套件的运行

这篇博客介绍了pytest框架的-x(--exitfirst)参数的使用,当测试用例运行时遇到失败,会立即停止执行,帮助开发者高效调试。文中通过一个包含4个测试用例的例子说明,当第三个用例失败时,第四个用例不会被执行,从而节省了在大型项目中排查问题的时间。
摘要由CSDN通过智能技术生成

关键字:-x(--exitfirst)

pytest参数 -x(--exitfirst),即在测试用例调试运行过程中,遇到用例失败,则停止运行,方便我们调试脚本,对于大型项目而言,可以节省很多时间。举例如下,共4个测试用例,其中第三个测试用例运行失败,第四个用例未执行:

import pytest


class TestPytestFeatures(object):
    def test_case_01(self):
        assert 1 == 1

    def test_case_02(self):
        assert isinstance(["a", "b"], list)

    def test_case_03(self):
        assert 1 == 4

    def test_case_04(self):
        assert 1 == 1


if __name__ == "__main__":
    pytest.main(["-x", "test_case_pytest_features.py"])

"""
运行结果
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:\icp_capp\help
plugins: allure-pytest-2.9.43
collected 4 items

test_case_pytest_features.py ..F

================================== FAILURES ===================================
_______________________ TestPytestFeatures.test_case_03 _______________________

self = <help.test_case_pytest_features.TestPytestFeatures object at 0x000001E15F9307F0>

    def test_case_03(self):
>       assert 1 == 4
E       assert 1 == 4

test_case_pytest_features.py:19: AssertionError
=========================== short test summary info ===========================
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_03 - asser...
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
========================= 1 failed, 2 passed in 0.09s =========================

Process finished with exit code 0

"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值