How to call method on every py.test assertion failure?
I personally haven't used but this might be your solution:https://pytest.org/latest/example/simple.html#writing-well-integrated-assertion-helpers
Also this could help:https://pytest.org/latest/assert.html#advanced-assertion-introspection
You have to use hooks. https://docs.pytest.org/en/latest/example/simple.html#post-process-test-reports-failures
@pytest.hookimpl(hookwrapper=True, tryfirst=True)def pytest_runtest_makereport(item, call): outcome = yield rep = outcome.get_result() setattr(item, "rep_" + rep.when, rep) return rep@pytest.fixture(autouse=True, scope='session')def driver(platform, request): """ some driver setup code """ yield driver if request.node.rep_call.failed: try: driver.get_screenshot_as_png() except: pass driver.quit()
And if you want to attach a screenshot to allure report, simply do:
@pytest.fixture(autouse=True, scope='session')def driver(platform, request): """ some driver setup code """ yield driver if request.node.rep_call.failed: # Make the screen-shot if test failed: try: allure.attach( driver.get_screenshot_as_png(), name=request.function.__name__, attachment_type=allure.attachment_type.PNG) except: """ do something """ driver.quit()
I think that screenShotInSelenium page should give you enough information regarding how you create a screenshot when an assert condition is met.
What you are missing is the use of @AfterMethod