Merge pull request #200 from robertbaldyga/fix-exceptions

tests: Handle pytest test state properly
This commit is contained in:
Michał Mielewczyk 2019-11-27 11:11:31 +01:00 committed by GitHub
commit e70a807839
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -94,11 +94,29 @@ def pytest_runtest_setup(item):
TestRun.LOGGER.start_group("Test body") 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(): def pytest_runtest_teardown():
""" """
This method is executed always in the end of each test, even if it fails or raises exception in This method is executed always in the end of each test, even if it fails or raises exception in
prepare stage. prepare stage.
""" """
if TestRun.outcome == "skipped":
return
TestRun.LOGGER.end_all_groups() TestRun.LOGGER.end_all_groups()
with TestRun.LOGGER.step("Cleanup after test"): with TestRun.LOGGER.step("Cleanup after test"):