当 pytest 中的会话装置失败时如何忽略测试

Mil*_*ála 6 python pytest

假设我有一个测试,如下所示:

import pytest
import copy

@pytest.fixture(scope='session')
def session_tool(request):
    tool = request.config.tool
    # Build is the critical part and may fail, raising an exception
    tool.build()
    return tool

@pytest.fixture
def tool(session_tool):
    return copy.deepcopy(session_tool)

def test_tool(tool, args):
    assert tool.run(args) == 0
Run Code Online (Sandbox Code Playgroud)

它构建一个会话范围的工具,然后为每个测试用例创建它的副本。但是,当构建失败时,session_tool下一个测试用例将再次执行固定装置,该测试用例再次失败......直到所有测试用例都失败。由于测试用例较多,因此需要一些时间才能完成该过程。

有没有办法告诉 pytest 跳过session_fixture第一次尝试构建失败后使用的所有测试?

Fra*_*k T 0

我可以想到两种方法:

1)调用pytest.skip()会导致测试被跳过。如果它也从固定装置内调用,则这也有效。就您而言,这将导致跳过所有剩余的测试。

2) 调用pytest.exit()将导致您的测试套件停止运行,就像触发了 KeyboardInterrupt 一样。