I would like to have a list of all the tests that have failed to be used at the end of session.
Pytest lets you define a hook pytest_sessionfinish(session, exitstatus)
, that is called at the end of the session, where I wish to have that list.
session
is a _pytest.main.Session
instance that has the attribute items
(type list
), but I couldn't find whether the each item
in that list passed of failed.
How can it be done while using pytest-xdist
plugin, where I would like to get that list in the master process. Using this plugin, session
does not even have items
attribute in the master:
def pytest_sessionfinish(session, exitstatus):
if os.environ.get("PYTEST_XDIST_WORKER", "master") == "master":
print(hasattr(session, "items")) # False
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.
The pytest-xdist plugin extends pytest with new test execution modes, the most used being distributing tests across multiple CPUs to speed up test execution: pytest -n auto.
Repeating a test Each test collected by pytest will be run count times. If you want to override default tests executions order, you can use --repeat-scope command line option with one of the next values: session , module , class or function (default). It behaves like a scope of the pytest fixture.
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 ==
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With