Skip to content

Commit f513d33

Browse files
authored
Modify documentation to use .stash when storing test results. (#10535)
1 parent 857e34e commit f513d33

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

doc/en/example/simple.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -895,29 +895,30 @@ here is a little example implemented via a local plugin:
895895
896896
import pytest
897897
898+
phase_report_key = StashKey[Dict[str, CollectReport]]()
899+
898900
899901
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
900902
def pytest_runtest_makereport(item, call):
901903
# execute all other hooks to obtain the report object
902904
outcome = yield
903905
rep = outcome.get_result()
904906
905-
# set a report attribute for each phase of a call, which can
907+
# store test results for each phase of a call, which can
906908
# be "setup", "call", "teardown"
907-
908-
setattr(item, "rep_" + rep.when, rep)
909+
item.stash.setdefault(phase_report_key, {})[rep.when] = rep
909910
910911
911912
@pytest.fixture
912913
def something(request):
913914
yield
914915
# request.node is an "item" because we use the default
915916
# "function" scope
916-
if request.node.rep_setup.failed:
917-
print("setting up a test failed!", request.node.nodeid)
918-
elif request.node.rep_setup.passed:
919-
if request.node.rep_call.failed:
920-
print("executing test failed", request.node.nodeid)
917+
report = request.node.stash[phase_report_key]
918+
if report["setup"].failed:
919+
print("setting up a test failed or skipped", request.node.nodeid)
920+
elif ("call" not in report) or report["call"].failed:
921+
print("executing test failed or skipped", request.node.nodeid)
921922
922923
923924
if you then have failing tests:

src/_pytest/tmpdir.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import Union
1313

1414
from _pytest.nodes import Item
15+
from _pytest.reports import CollectReport
1516
from _pytest.stash import StashKey
1617

1718
if TYPE_CHECKING:
@@ -318,9 +319,7 @@ def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]):
318319
@hookimpl(tryfirst=True, hookwrapper=True)
319320
def pytest_runtest_makereport(item: Item, call):
320321
outcome = yield
321-
result = outcome.get_result()
322+
result: CollectReport = outcome.get_result()
322323

323-
if tmppath_result_key not in item.stash:
324-
item.stash[tmppath_result_key] = {result.when: result.passed}
325-
else:
326-
item.stash[tmppath_result_key][result.when] = result.passed
324+
empty: Dict[str, bool] = {}
325+
item.stash.setdefault(tmppath_result_key, empty)[result.when] = result.passed

0 commit comments

Comments
 (0)