Skip to content

Modify documentation to use .stash when storing test results. #10535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions doc/en/example/simple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -895,29 +895,30 @@ 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):
# 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
# 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
def something(request):
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:
Expand Down
9 changes: 4 additions & 5 deletions src/_pytest/tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Union

from _pytest.nodes import Item
from _pytest.reports import CollectReport
from _pytest.stash import StashKey

if TYPE_CHECKING:
Expand Down Expand Up @@ -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