Skip to content Skip to sidebar Skip to footer

In Pytest, How Can I Figure Out If A Test Failed? (from "request")

I'm using Selenium with PYTEST to test a site. I would like to take a screenshot of the page whenever a test fails (and only when it fails). Is there a way that I can do this? The

Solution 1:

It can be done, not directly though. I just added an example to the docs. It probably makes sense to makes this easier by default, i.e. without requiring the use of a conftest.py hook. If you agree, please file an issue.

Solution 2:

I had to do something similar on a per-module level. After examining the existing solutions I was a little surprised by their complexity. Here's an approach I came up with to address this issue:

import pytest


@pytest.fixture(scope="module", autouse=True)deffailure_tracking_fixture(request):
    tests_failed_before_module = request.session.testsfailed
    yield
    tests_failed_during_module = request.session.testsfailed - tests_failed_before_module

It can be tweaked to do what you want by making the fixture a function-level one.

Hope this helps!

Post a Comment for "In Pytest, How Can I Figure Out If A Test Failed? (from "request")"