python 测试用例的无输入_python – 使用pytest的参数化,如果一个测试用例失败,如何跳过剩余的测试?...

在执行测试之前评估标记,因此无法传递某种依赖于其他测试结果的声明性标记(如skipif).但是,您可以在钩子中应用自定义测试跳过逻辑.从pytest docs修改

incremental testing – test steps配方:

# conftest.py

import pytest

def pytest_sessionstart(session):

session.failednames = set()

def pytest_runtest_makereport(item, call):

if call.excinfo is not None:

item.session.failednames.add(item.originalname)

def pytest_runtest_setup(item):

if item.originalname in item.session.failednames:

pytest.skip("previous test failed (%s)" % item.name) # or use pytest.xfail like in the other answer

示例测试

@pytest.mark.parametrize('i', range(10))

def test_spam(i):

assert i != 3

收益率:

=================================== test session starts ===================================

collected 10 items

test_spam.py::test_spam[0] PASSED

test_spam.py::test_spam[1] PASSED

test_spam.py::test_spam[2] PASSED

test_spam.py::test_spam[3] FAILED

test_spam.py::test_spam[4] SKIPPED

test_spam.py::test_spam[5] SKIPPED

test_spam.py::test_spam[6] SKIPPED

test_spam.py::test_spam[7] SKIPPED

test_spam.py::test_spam[8] SKIPPED

test_spam.py::test_spam[9] SKIPPED

========================================= FAILURES ========================================

_______________________________________ test_spam[3] ______________________________________

i = 3

@pytest.mark.parametrize('i', range(10))

def test_spam(i):

> assert i != 3

E assert 3 != 3

test_spam.py:5: AssertionError

====================== 1 failed, 3 passed, 6 skipped in 0.06 seconds ======================

编辑:使用自定义标记

def pytest_runtest_makereport(item, call):

markers = {marker.name for marker in item.iter_markers()}

if call.excinfo is not None and 'skiprest' in markers:

item.session.failednames.add(item.originalname)

def pytest_runtest_setup(item):

markers = {marker.name for marker in item.iter_markers()}

if item.originalname in item.session.failednames and 'skiprest' in markers:

pytest.skip(item.name)

用法:

@pytest.mark.skiprest

@pytest.mark.parametrize('somearg', ['a', 'b', 'c'])

def test_marked(somearg):

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值