Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 555
Nacht Avatar asked Nov 13 '12 16:11

Nacht


People also ask

How do you run failed test cases in pytest?

The plugin provides two command line options to rerun failures from the last pytest invocation: --lf , --last-failed - to only re-run the failures. --ff , --failed-first - to run the failures first and then the rest of the tests.

How do I know if pytest ignores a file?

Ignore paths during test collection The --ignore-glob option allows to ignore test file paths based on Unix shell-style wildcards. If you want to exclude test-modules that end with _01.py , execute pytest with --ignore-glob='*_01.py' .

How pytest identifies the test files and test methods?

Pytest automatically identifies those files as test files. We can make pytest run other filenames by explicitly mentioning them. Pytest requires the test function names to start with test. Function names which are not of format test* are not considered as test functions by pytest.

How do you Screenshot failed test cases in selenium pytest?

In this example, Selenium will navigate to a specific URL (using the Chrome browser) and then take a screenshot using the save_screenshot() function. It will then display the screenshot using pillow. While taking the screenshot, testers need Selenium WebDriver to open a web browser automatically.


2 Answers

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.

like image 151
hpk42 Avatar answered Sep 16 '22 18:09

hpk42


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!

like image 30
Vladimirs Avatar answered Sep 20 '22 18:09

Vladimirs