diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index e62060f19..a528e50ef 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -895,6 +895,8 @@ here is a little example implemented via a local plugin: import pytest + phase_report_key = StashKey[Dict[str, CollectReport]]() + @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): @@ -902,10 +904,9 @@ here is a little example implemented via a local plugin: outcome = yield rep = outcome.get_result() - # set a report attribute for each phase of a call, which can + # store test results for each phase of a call, which can # be "setup", "call", "teardown" - - setattr(item, "rep_" + rep.when, rep) + item.stash.setdefault(phase_report_key, {})[rep.when] = rep @pytest.fixture @@ -913,11 +914,11 @@ here is a little example implemented via a local plugin: yield # request.node is an "item" because we use the default # "function" scope - if request.node.rep_setup.failed: - print("setting up a test failed!", request.node.nodeid) - elif request.node.rep_setup.passed: - if request.node.rep_call.failed: - print("executing test failed", request.node.nodeid) + report = request.node.stash[phase_report_key] + if report["setup"].failed: + print("setting up a test failed or skipped", request.node.nodeid) + elif ("call" not in report) or report["call"].failed: + print("executing test failed or skipped", request.node.nodeid) if you then have failing tests: diff --git a/src/_pytest/tmpdir.py b/src/_pytest/tmpdir.py index 3fd8168b6..48670b6c4 100644 --- a/src/_pytest/tmpdir.py +++ b/src/_pytest/tmpdir.py @@ -12,6 +12,7 @@ from typing import TYPE_CHECKING from typing import Union from _pytest.nodes import Item +from _pytest.reports import CollectReport from _pytest.stash import StashKey if TYPE_CHECKING: @@ -318,9 +319,7 @@ def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]): @hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item: Item, call): outcome = yield - result = outcome.get_result() + result: CollectReport = outcome.get_result() - if tmppath_result_key not in item.stash: - item.stash[tmppath_result_key] = {result.when: result.passed} - else: - item.stash[tmppath_result_key][result.when] = result.passed + empty: Dict[str, bool] = {} + item.stash.setdefault(tmppath_result_key, empty)[result.when] = result.passed