I am trying to implement a new pytest
marker, called @pytest.mark.must_pass
, to indicate that if the marked test fails, pytest should skip all subsequent tests and terminate.
I have been able to use the pytest_runtest_call
hook to get pytest to terminate if the marked test failed, but I am using pytest.exit
, which does not print a traceback, nor does it show the failure indication for the test in question.
I need this failure to appear as any other test failure, except that pytest stops testing after it prints whatever it needs to print to detail the failure.
My code so far:
# 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
Is there already a mechanism for doing this built into pytest?
Any help will be greatly appreciated.
Thanks.