From 83ab8be5f60baeef7f84086617c5144f5124a4af Mon Sep 17 00:00:00 2001 From: Yusuke Kadowaki Date: Sat, 26 Nov 2022 23:17:31 +0900 Subject: [PATCH] Mention to use stash in the doc --- doc/en/example/simple.rst | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index e62060f19..d0ad693b0 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 + your_unique_key = StashKey[Dict[str, bool]]() + @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): @@ -902,10 +904,12 @@ 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) + if your_unique_key not in item.stash: + item.stash[your_unique_key] = {rep.when: rep.passed} + else: + item.stash[your_unique_key][rep.when] = rep.passed @pytest.fixture @@ -913,11 +917,13 @@ 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: + result = request.node.stash[your_unique_key] + if not result.get("setup", False): 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) + elif not result.get("call", False): + print("executing test failed", request.node.nodeid) + elif not result("teardown", False): + print("tear down test failed", request.node.nodeid) if you then have failing tests: