跳转到主要内容

调用runtime_xfail()将运行中的测试标记为xfail。

项目描述

pytest-runtime-xfail

pytest插件,提供runtime_xfail固定装置,可通过runtime_xfail()调用,允许在运行时决定将测试标记为xfail

安装

使用pip安装

pip install pytest-runtime-xfail

使用方法

包含固定装置,然后调用它以在运行时将测试标记为xfail

 def test_something(runtime_xfail):
     if (runtime_condition):
        runtime_xfail()
     # ... the rest of your test

当然也可以在固定装置中使用。

@pytest.fixture()
def foo(runtime_xfail):
  if (runtime_condition):
     runtime_xfail()
  # ... the rest of your fixture

def test_something(foo):
  # ... the rest of your test

此插件为何需要

pytest允许您通过两种方式将测试标记为预期失败,或xfail。

  1. @pytest.mark.xfail。这允许您在测试收集时间将测试或测试参数化标记为xfail

    • pytest像对待任何其他测试一样运行标记为xfail的测试。
    • 如果测试失败,它将导致XFAIL
    • 如果它通过,则XPASS。除非您有xfail_strict=true@pytest.mark.xfail(strict=True),在这种情况下,通过xfail标记的测试将导致FAIL
      • 这有助于在预期的失败测试开始通过时发出警告。
  2. pytest.xfail()。如果您需要仅在运行时已知的信息来决定是否标记为xfail,您可以在测试或固定装置中调用pytest.xfail()

    • pytest会像平常一样运行测试,直到调用pytest.xfail()
    • 当调用pytest.xfail()时,测试执行停止,测试结果为XFAIL
    • 不会运行测试的其余部分。
    • 无法从pytest.xfail()获得XPASS
    • xfail_strict没有效果。

有时我们希望结合这些行为。

  • 我们不知道在运行时是否应该将测试标记为xfail
  • 我们想要进行测试运行。
  • 我们希望能够得到XFAILXPASS两种结果的可能性。
  • 我们希望能够通过设置xfail_strict=true在测试开始通过时得到警报。

这个插件填补了这一空白。

替代方案

您可以通过通过在requests对象中添加标记来绕过相同的限制。

def test_something(request):
     if (runtime_condition): 
        request.node.add_marker(pytest.mark.xfail(reason='some reason'))
     # ... rest of test

这正是这个插件所做的事情,只是在 fixture 中。

示例请参考 example/test_xfail.py

"""
Run this with
* pytest -v
* pytest -v -o xfail_strict=true
"""

import pytest

@pytest.mark.xfail()
def test_marker_pass():
    'Can be XPASS or FAIL (if xfail_strict)'
    assert True

@pytest.mark.xfail()
def test_marker_fail():
    'Will always be XFAIL'
    assert False  # this statememt will be run

def test_old_xfail_pass():
    'Will always be XFAIL'
    pytest.xfail()
    assert True  # this statememt will NOT be run

def test_old_xfail_fail():
    'Will always be XFAIL'
    pytest.xfail()
    assert False  # this statememt will NOT be run

def test_runtime_xfail_pass(runtime_xfail):
    runtime_xfail()
    assert True  # this statement will be run

def test_runtime_xfail_fail(runtime_xfail):
    runtime_xfail()
    assert False  # this statement will be run

def test_runtime_xfail_reason(runtime_xfail):
    runtime_xfail(reason="for demo")
    assert False  # this statement will be run

输出

(venv) $ pytest -v test_xfail.py 
========================= test session starts ==========================
collected 7 items                                                      

test_xfail.py::test_marker_pass XPASS                            [ 14%]
test_xfail.py::test_marker_fail XFAIL                            [ 28%]
test_xfail.py::test_old_xfail_pass XFAIL                         [ 42%]
test_xfail.py::test_old_xfail_fail XFAIL                         [ 57%]
test_xfail.py::test_runtime_xfail_pass XPASS                     [ 71%]
test_xfail.py::test_runtime_xfail_fail XFAIL                     [ 85%]
test_xfail.py::test_runtime_xfail_reason XFAIL (for demo)        [100%]

==================== 5 xfailed, 2 xpassed in 0.05s =====================
(venv) $ pytest -v test_xfail.py -o xfail_strict=true
========================= test session starts ==========================
collected 7 items                                                      

test_xfail.py::test_marker_pass FAILED                           [ 14%]
test_xfail.py::test_marker_fail XFAIL                            [ 28%]
test_xfail.py::test_old_xfail_pass XFAIL                         [ 42%]
test_xfail.py::test_old_xfail_fail XFAIL                         [ 57%]
test_xfail.py::test_runtime_xfail_pass FAILED                    [ 71%]
test_xfail.py::test_runtime_xfail_fail XFAIL                     [ 85%]
test_xfail.py::test_runtime_xfail_reason XFAIL (for demo)        [100%]

===================== 2 failed, 5 xfailed in 0.04s =====================

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

pytest-runtime-xfail-1.0.3.tar.gz (4.9 kB 查看哈希值)

上传时间 源代码

构建分发

pytest_runtime_xfail-1.0.3-py3-none-any.whl (3.8 kB 查看哈希值)

上传时间 Python 3

支持者