Browse Source

pytest: Pass result to fixtures and keep directories of failed tests

@Reported-by: Rusty Russell <@rustyrussell>
@Signed-off-by: Christian Decker <@cdecker>
ppa-0.6.1
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
b55d03cb30
  1. 18
      tests/conftest.py
  2. 7
      tests/fixtures.py

18
tests/conftest.py

@ -0,0 +1,18 @@
import pytest
# This function is based upon the example of how to
# "[make] test result information available in fixtures" at:
# https://pytest.org/latest/example/simple.html#making-test-result-information-available-in-fixtures
# and:
# https://github.com/pytest-dev/pytest/issues/288
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
# execute all other hooks to obtain the report object
outcome = yield
rep = outcome.get_result()
# set a report attribute for each phase of a call, which can
# be "setup", "call", "teardown"
setattr(item, "rep_" + rep.when, rep)

7
tests/fixtures.py

@ -35,7 +35,7 @@ def test_base_dir():
@pytest.fixture
def directory(test_base_dir, test_name):
def directory(request, test_base_dir, test_name):
"""Return a per-test specific directory.
This makes a unique test-directory even if a test is rerun multiple times.
@ -48,7 +48,12 @@ def directory(test_base_dir, test_name):
yield directory
# This uses the status set in conftest.pytest_runtest_makereport to
# determine whether we succeeded or failed.
if request.node.rep_call.outcome == 'passed':
shutil.rmtree(directory)
else:
logging.debug("Test execution failed, leaving the test directory {} intact.".format(directory))
@pytest.fixture

Loading…
Cancel
Save