繁体   English   中英

将测试标记为从 pytest 中的夹具内部通过

[英]Mark test as passed from inside a fixture in pytest

使用pytest.skippytest.xfail ,我可以从夹具内部将测试标记为已跳过或 xfailed。 但是,没有pytest.pass 如何将其标记为已通过?

import pytest

@pytest.fixture
def fixture():
    #pytest.skip()
    pytest.xfail()

def test(fixture):
    assert False

不幸的是,我不知道如何通过夹具通过测试,但是您可以在测试中使用 pytest.skip 和 msg 例如“pass”,conftest.py 中的钩子将检查此“通过”msg 并使测试通过:

比赛.py

import pytest


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(call):
    outcome = yield
    if outcome.get_result().outcome == 'skipped' and call.excinfo.value.msg == 'pass':
        outcome.get_result().outcome = 'passed'

test_skip.py

# -*- coding: utf-8 -*-
import pytest


def test_skip_pass():
    pytest.skip('pass')


def test_skip_pass_2():
    pytest.skip('skip')

结果:

正在收集...收集了 2 个项目

test_skip.py::test_skip_pass 通过 [50%]
test_skip.py::test_skip_pass_2 已跳过 [100%]
跳过:跳过

======================== 1个通过,1个在0.04秒内跳过================== =======

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM