Android – Executes code if the py.test test fails

Executes code if the py.test test fails… here is a solution to the problem.

Executes code if the py.test test fails

I’m using Appium for UI test automation on Android py.test adb save error report after test failure.

Is there a way to tell if a test in my test code has failed so that I can run and save the error report in teardown?

Originally, I just wanted to save the bug report after each test, but adding 45 seconds to each test is a bit excessive.

Solution

You can implement a pytest_runtest_logreport Hook in your conftest.py like this

def pytest_runtest_logreport(report):
    if report.when == 'call' and report.failed:
        # save bug report

For more information, see Woking with plugins and conftest files

Related Problems and Solutions