Skip to content

Instantly share code, notes, and snippets.

@jlaska
Created November 14, 2013 21:37
Show Gist options
  • Save jlaska/7474758 to your computer and use it in GitHub Desktop.
Save jlaska/7474758 to your computer and use it in GitHub Desktop.
Pytest plugin to enable test scenarios. A test scenario is a series of related tests where sequential successful execution is important.
import py
import pytest
'''
Automatically xfail remaining tests if previous test failed. Tests will
continue to run if:
* the result is skip
* the result is xfail
* the test is marked as nondestructive
For more information, refer to
http://stackoverflow.com/questions/12411431/pytest-how-to-skip-the-rest-of-tests-in-the-class-if-one-has-failed/12579625#12579625
'''
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords \
and "destructive" in item.keywords \
and call.excinfo is not None \
and not any([call.excinfo.errisinstance(py.test.xfail.Exception),
call.excinfo.errisinstance(py.test.skip.Exception)]):
parent = item.parent
parent._previousfailed = item
def pytest_runtest_setup(item):
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed (%s)" %previousfailed.name)
@ml31415
Copy link

ml31415 commented May 11, 2015

Thanks for sharing, exactly what I was looking for. One hint:

    call.excinfo.errisinstance((pytest.xfail.Exception, pytest.skip.Exception)):

does the trick and is easier to read. It's a simple instance check, which also accepts tuples of several types in question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment