finefoot
finefoot

Reputation: 11302

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. There is no pytest.pass, though. How can I mark it as passed?

import pytest

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

def test(fixture):
    assert False

Upvotes: 2

Views: 2332

Answers (1)

vadspb
vadspb

Reputation: 81

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:

conftest.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')

result:

collecting ... collected 2 items

test_skip.py::test_skip_pass PASSED [ 50%]
test_skip.py::test_skip_pass_2 SKIPPED [100%]
Skipped: skip

======================== 1 passed, 1 skipped in 0.04s =========================

Upvotes: 1

Related Questions