@@ -212,6 +212,39 @@ def pytest_collection_modifyitems(items, *args, **kwargs):
212212 item .add_marker ("fast" )
213213
214214
215+ @pytest .hookimpl (hookwrapper = True , tryfirst = True )
216+ def pytest_runtest_makereport (item : pytest .Item , call : pytest .CallInfo ):
217+ # The tmp_path fixture frequently throws errors like:
218+ # - KeyError: <_pytest.stash.StashKey object at 0x79ba385fe1a0>
219+ # in its teardown. This causes pytest to mark the test as failed even though we have zero control over this behaviour.
220+ # So we log/swallow that particular error here rather than raising it
221+
222+ # note: the hook always has to yield
223+ outcome = yield
224+
225+ # we only care about tests that used the tmp_path fixture
226+ if "tmp_path" not in getattr (item , "fixturenames" , []):
227+ return
228+
229+ result : pytest .TestReport = outcome .get_result ()
230+
231+ if result .when != "teardown" :
232+ return
233+
234+ # If we specifically failed with a StashKey error in teardown, mark the test as passed
235+ if result .failed :
236+ exception = call .excinfo
237+ if (
238+ exception
239+ and isinstance (exception .value , KeyError )
240+ and "_pytest.stash.StashKey" in repr (exception )
241+ ):
242+ result .outcome = "passed"
243+ item .add_report_section (
244+ "teardown" , "stderr" , f"Ignored tmp_path teardown error: { exception } "
245+ )
246+
247+
215248# Ignore all local config files
216249@pytest .fixture (scope = "session" , autouse = True )
217250def ignore_local_config_files ():
0 commit comments