From b55d03cb3024d3423f7d78c8436559bec5f33c38 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 1 Aug 2018 16:18:01 +0200 Subject: [PATCH] pytest: Pass result to fixtures and keep directories of failed tests @Reported-by: Rusty Russell <@rustyrussell> @Signed-off-by: Christian Decker <@cdecker> --- tests/conftest.py | 18 ++++++++++++++++++ tests/fixtures.py | 9 +++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..59ad4da25 --- /dev/null +++ b/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) diff --git a/tests/fixtures.py b/tests/fixtures.py index d65a19a4d..a1392a303 100644 --- a/tests/fixtures.py +++ b/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 - shutil.rmtree(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