【发布时间】:2020-04-10 06:16:49
【问题描述】:
我正在尝试实现一个新的pytest 标记,称为@pytest.mark.must_pass,以指示如果标记的测试失败,pytest 应该跳过所有后续测试并终止。
如果标记的测试失败,我已经能够使用pytest_runtest_call 挂钩让 pytest 终止,但我使用的是pytest.exit,它不会打印回溯,也不会显示测试的失败指示有问题。
我需要将此失败显示为任何其他测试失败,除了 pytest 在打印需要打印的任何内容以详细说明失败后停止测试。
到目前为止我的代码:
# Copied this implementation from _pytest.runner
def pytest_runtest_call(item):
_update_current_test_var(item, "call")
try:
del sys.last_type
del sys.last_value
del sys.last_traceback
except AttributeError:
pass
try:
item.runtest()
except Exception:
# Store trace info to allow postmortem debugging
type, value, tb = sys.exc_info()
assert tb is not None
tb = tb.tb_next # Skip *this* frame
sys.last_type = type
sys.last_value = value
sys.last_traceback = tb
del type, value, tb # Get rid of these in this frame
# If test is marked as must pass, stop testing here
if item.iter_markers(name = "must_pass"):
pytest.exit('Test marked as "must_pass" failed, terminating.')
raise
pytest 中是否已经有这样做的机制?
任何帮助将不胜感激。
谢谢。
【问题讨论】:
-
根据您提供的代码,一旦您到达
must_pass标记,pytest 将始终退出 -
@aws_apprentice 是的,但它不会显示为失败的测试。例如,它不会显示回溯,也不会以红色打印“FAILED”。
-
那么,如果遇到此标记不再运行任何测试而只是停止,那么所需的行为到底是什么?
-
是的,当标记为“必须通过”的测试失败时,它应该将失败显示为任何其他测试失败,并跳过所有剩余的测试,然后退出。