Pytest: how to take action on test failure?

Implement a pytest_exception_interact in a conftest.py file, which according to the documentation:

called when an exception was raised which can potentially be interactively handled.

def pytest_exception_interact(node, call, report):
    if report.failed:
        # call.excinfo contains an ExceptionInfo instance

It's not clear from your question exactly what you want to gather from the error, but probably having access to an ExceptionInfo instance should be enough for your case.


You can also do it in the second half of a yield fixture:

https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures

Tags:

Pytest