简体   繁体   English

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

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

With pytest.skip or pytest.xfail , I can mark a test as skipped or xfailed from inside the fixture.使用pytest.skippytest.xfail ,我可以从夹具内部将测试标记为已跳过或 xfailed。 There is no pytest.pass , though.但是,没有pytest.pass How can I mark it as passed?如何将其标记为已通过?

import pytest

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

def test(fixture):
    assert False

Unfortunately i don't know the way to pass tests with fixtures, but you can use pytest.skip in your test with msg for example "pass" and the hook from conftest.py will check this "pass" msg and make test passed:不幸的是,我不知道如何通过夹具通过测试,但是您可以在测试中使用 pytest.skip 和 msg 例如“pass”,conftest.py 中的钩子将检查此“通过”msg 并使测试通过:

conftest.py比赛.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 test_skip.py

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


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


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

result:结果:

collecting... collected 2 items正在收集...收集了 2 个项目

test_skip.py::test_skip_pass PASSED [ 50%] test_skip.py::test_skip_pass 通过 [50%]
test_skip.py::test_skip_pass_2 SKIPPED [100%] test_skip.py::test_skip_pass_2 已跳过 [100%]
Skipped: skip跳过:跳过

======================== 1 passed, 1 skipped in 0.04s ========================= ======================== 1个通过,1个在0.04秒内跳过================== =======

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

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