python - 将测试标记为从 pytest 中的 fixture 内部通过

标签 python unit-testing testing pytest python-unittest

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/

相关文章:

python - ValueError:时间数据 '02/03/2022' 与格式 '%d/%m/%y ' 不匹配

c++ - boost::unit_test 案例失败,因为子进程以非零值退出

testing - 测试失败时的 Helm 部署状态

testing - 如何获得数据流图以及 C 代码的 c-use 和 p-use 变量?

c# - 如何在 NUnit 3.2 中创建全局初始化?

python - `pip install pyside` 在 Linux 上卡住?

java - 使用 Java 命名约定将 Python 对象(使用 Python 约定)编码为 JSON

python - HTTP实时音频流服务器

node.js - 仅对 Node 中的 chai-http 单元测试进行 1 个 http 调用?

c# - Membership.CreateUser 的 ASP.NET MVC 3 单元测试总是返回 MembershipCreateStatus 错误