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

This commit is contained in:
Yusuke Kadowaki 2022-11-28 05:09:56 +09:00 committed by GitHub
parent 857e34ef85
commit f513d33d5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 13 deletions

View File

@ -895,6 +895,8 @@ here is a little example implemented via a local plugin:
import pytest import pytest
phase_report_key = StashKey[Dict[str, CollectReport]]()
@pytest.hookimpl(tryfirst=True, hookwrapper=True) @pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call): def pytest_runtest_makereport(item, call):
@ -902,10 +904,9 @@ here is a little example implemented via a local plugin:
outcome = yield outcome = yield
rep = outcome.get_result() 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" # be "setup", "call", "teardown"
item.stash.setdefault(phase_report_key, {})[rep.when] = rep
setattr(item, "rep_" + rep.when, rep)
@pytest.fixture @pytest.fixture
@ -913,11 +914,11 @@ here is a little example implemented via a local plugin:
yield yield
# request.node is an "item" because we use the default # request.node is an "item" because we use the default
# "function" scope # "function" scope
if request.node.rep_setup.failed: report = request.node.stash[phase_report_key]
print("setting up a test failed!", request.node.nodeid) if report["setup"].failed:
elif request.node.rep_setup.passed: print("setting up a test failed or skipped", request.node.nodeid)
if request.node.rep_call.failed: elif ("call" not in report) or report["call"].failed:
print("executing test failed", request.node.nodeid) print("executing test failed or skipped", request.node.nodeid)
if you then have failing tests: if you then have failing tests:

View File

@ -12,6 +12,7 @@ from typing import TYPE_CHECKING
from typing import Union from typing import Union
from _pytest.nodes import Item from _pytest.nodes import Item
from _pytest.reports import CollectReport
from _pytest.stash import StashKey from _pytest.stash import StashKey
if TYPE_CHECKING: if TYPE_CHECKING:
@ -318,9 +319,7 @@ def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]):
@hookimpl(tryfirst=True, hookwrapper=True) @hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item: Item, call): def pytest_runtest_makereport(item: Item, call):
outcome = yield outcome = yield
result = outcome.get_result() result: CollectReport = outcome.get_result()
if tmppath_result_key not in item.stash: empty: Dict[str, bool] = {}
item.stash[tmppath_result_key] = {result.when: result.passed} item.stash.setdefault(tmppath_result_key, empty)[result.when] = result.passed
else:
item.stash[tmppath_result_key][result.when] = result.passed