pytest-subtests icon indicating copy to clipboard operation
pytest-subtests copied to clipboard

Feature Request: Programmatically stop execution upon first failed subtest

Open rhoban13 opened this issue 11 months ago • 0 comments

Consider adding a flag on the SubTests class which would allow the same behavior as --exitfirst, but scoped within a test scope.

Proposed usage may look something like:

def test_foo(subtests):
    subtests.stop_upon_failure = True  # new flag

    with subtests.test("sub1"):
        assert False, "This sub test failed"
    with subtests.test("sub2"):  # This would not get executed
        ...    

Use cases:

  1. Using subtests to distinguish steps within a workflow, somewhat analogous to what happens with gherkin style frameworks like pytest-bdd. There may be no reason to continue with subsequent steps when one fails

  2. Using subtests to distinguish "fatal asserts" from "non fatal" ones.

Alternatives: One could achieve the same result by not using a subtest for those fatal steps, instead doing those asserts in the enclosing test scope. The downside of that is purely readability. In the example above, it may be desirable to simply have the body of "sub1" indented into its own block.

rhoban13 avatar Jan 29 '25 16:01 rhoban13