pytest: How to get a list of all failed tests at the end of the session? (and while using xdist)

Run pytest with -rf to get it to print a list of failed tests at the end.

From py.test --help:

  -r chars              show extra test summary info as specified by chars
                        (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed,
                        (p)passed, (P)passed with output, (a)all except pP.
                        Warnings are displayed at all times except when
                        --disable-warnings is set

Here's what you get:

$ py.test -rf
================= test session starts =================
platform darwin -- Python 3.7.2, pytest-4.3.1, py-1.6.0, pluggy-0.7.1
[...]
=============== short test summary info ===============
FAILED test_foo.py::test_foo_is_flar
FAILED test_spam.py::test_spam_is_mostly_pork
FAILED test_eggs.py::test_eggs_are_also_spam
=== 3 failed, 222 passed, 8 warnings in 12.52 seconds ==

You can use command line option --result-log:

test_dummy.py:

def test_dummy_success():
    return


def test_dummy_fail():
    raise Exception('Dummy fail')

Command line:

$ py.test --result-log=test_result.txt

Content of the test_result.txt

. test_dummy.py::test_dummy_success
F test_dummy.py::test_dummy_fail
 def test_dummy_fail():
 >       raise Exception('Dummy fail')
 E       Exception: Dummy fail

 test_dummy.py:6: Exception

Just search 'F' in first column and after that would be [file]::[test]


If you want results of the tests you can use hook runtest_makereport:

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    if rep.when == 'call' and rep.failed:
        mode = 'a' if os.path.exists('failures') else 'w'
        try:  # Just to not crash py.test reporting
          pass  # the test 'item' failed
        except Exception as e:
            pass