From 116d13364b9bc7105b54f88f9b6d4767a86af2ec Mon Sep 17 00:00:00 2001 From: Robert Baldyga Date: Tue, 26 Nov 2019 17:30:03 +0100 Subject: [PATCH] tests: Handle pytest test state properly - Log information about test fail in case of exception. - Skip teardown in case of test skip (setup is skipped by default). Signed-off-by: Robert Baldyga --- test/functional/tests/conftest.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/functional/tests/conftest.py b/test/functional/tests/conftest.py index b3b8db9..e7dda91 100644 --- a/test/functional/tests/conftest.py +++ b/test/functional/tests/conftest.py @@ -84,11 +84,29 @@ def pytest_runtest_setup(item): TestRun.LOGGER.start_group("Test body") +@pytest.hookimpl(tryfirst=True, hookwrapper=True) +def pytest_runtest_makereport(item, call): + res = (yield).get_result() + + TestRun.outcome = res.outcome + + from _pytest.outcomes import Failed + if res.when == "call" and res.failed: + msg = f"{call.excinfo.type.__name__}: {call.excinfo.value}" + if call.excinfo.type is Failed: + TestRun.LOGGER.error(msg) + else: + TestRun.LOGGER.exception(msg) + + def pytest_runtest_teardown(): """ This method is executed always in the end of each test, even if it fails or raises exception in prepare stage. """ + if TestRun.outcome == "skipped": + return + TestRun.LOGGER.end_all_groups() with TestRun.LOGGER.step("Cleanup after test"):