与 pytest.skip
或 pytest.xfail
,我可以从 fixture 内部将测试标记为已跳过或 xfailed。没有pytest.pass
, 尽管。如何将其标记为通过?
import pytest
@pytest.fixture
def fixture():
#pytest.skip()
pytest.xfail()
def test(fixture):
assert False
最佳答案
不幸的是,我不知道通过 fixture 通过测试的方法,但是您可以在测试中使用 pytest.skip 使用 msg 例如“pass”,来自 conftest.py 的钩子(Hook)将检查此“pass”msg并使测试通过:
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')
结果: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 =========================
关于python - 将测试标记为从 pytest 中的 fixture 内部通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61601899/