调用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。
-
@pytest.mark.xfail
。这允许您在测试收集时间将测试或测试参数化标记为xfail
。- pytest像对待任何其他测试一样运行标记为
xfail
的测试。 - 如果测试失败,它将导致
XFAIL
。 - 如果它通过,则
XPASS
。除非您有xfail_strict=true
或@pytest.mark.xfail(strict=True)
,在这种情况下,通过xfail标记的测试将导致FAIL
。- 这有助于在预期的失败测试开始通过时发出警告。
- pytest像对待任何其他测试一样运行标记为
-
pytest.xfail()
。如果您需要仅在运行时已知的信息来决定是否标记为xfail
,您可以在测试或固定装置中调用pytest.xfail()
。- pytest会像平常一样运行测试,直到调用
pytest.xfail()
。 - 当调用
pytest.xfail()
时,测试执行停止,测试结果为XFAIL
。 - 不会运行测试的其余部分。
- 无法从
pytest.xfail()
获得XPASS
。 xfail_strict
没有效果。
- pytest会像平常一样运行测试,直到调用
有时我们希望结合这些行为。
- 我们不知道在运行时是否应该将测试标记为
xfail
。 - 我们想要进行测试运行。
- 我们希望能够得到
XFAIL
和XPASS
两种结果的可能性。 - 我们希望能够通过设置
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.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5eefc17e499bd639ddc2734c588166b2f59649454cd5921efb9243268894f09d |
|
MD5 | c025a2ef69925eb0bd58db3cf0566834 |
|
BLAKE2b-256 | af73a12ad0ff23f78e5a857fa5760dc4c70624b18d383cb253e0ebfa3d194ae0 |
关闭
pytest_runtime_xfail-1.0.3-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 84aa805f8a5beb82a6f26a018ce5502909cb27a0193c9e5c7123bcd5fbb6787b |
|
MD5 | 4f717496a53129617a14b69f4768a760 |
|
BLAKE2b-256 | 952036bab381f826b0428c8b0db6253ca5930789a90584254b8e08ea097fe432 |