In pytest, how can I figure out if a test failed? (from "request")

10,557

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)
def failure_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!

Share:
10,557
Nacht
Author by

Nacht

Updated on June 12, 2022

Comments

  • Nacht
    Nacht almost 2 years

    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 docs are quiet when it comes to this (or I can't find it). I would assume that it would be something like

    request.function.failed
    

    and it would return a boolean or something.

    This is what I wanted to do:

    @pytest.fixture()
    def something(request):
        if request.function.failed:
            print "I failed"
    

    This would be added to a finalizer, of course. Can it be done? Using pytest 2.3.3

    Thanks.

  • Nacht
    Nacht over 11 years
    Filed it. Not sure if I gave enough info (assigned to you).
  • Eligio Mariño
    Eligio Mariño about 9 years
    NB issue is now at bitbucket.org/pytest-dev/pytest/issue/230/… (repo was moved)
  • Tomer Gal
    Tomer Gal over 4 years
    This solution does not deal with tests running in parallel, it's behavior will be unpredictable.
  • Vladimirs
    Vladimirs about 4 years
    I don't have much context on how tests are run in parallel in pytest but I suspect there is an easy way to safely share state across parallel runs in a similar fashion. Good point though.
  • Benno
    Benno over 3 years
    And the issue is now here: github.com/pytest-dev/pytest/issues/230