@@ -142,6 +142,43 @@ def raiser(exc):
142142 assert isinstance (func .exceptions [0 ], TypeError ) # type: ignore
143143 assert isinstance (func .exceptions [1 ], ValueError ) # type: ignore
144144
145+ def test_cached_exception_doesnt_get_longer (self , pytester : Pytester ) -> None :
146+ """Regression test for #12204 (the "BTW" case)."""
147+ pytester .makepyfile (test = "" )
148+ # If the collector.setup() raises, all collected items error with this
149+ # exception.
150+ pytester .makeconftest (
151+ """
152+ import pytest
153+
154+ class MyItem(pytest.Item):
155+ def runtest(self) -> None: pass
156+
157+ class MyBadCollector(pytest.Collector):
158+ def collect(self):
159+ return [
160+ MyItem.from_parent(self, name="one"),
161+ MyItem.from_parent(self, name="two"),
162+ MyItem.from_parent(self, name="three"),
163+ ]
164+
165+ def setup(self):
166+ 1 / 0
167+
168+ def pytest_collect_file(file_path, parent):
169+ if file_path.name == "test.py":
170+ return MyBadCollector.from_parent(parent, name='bad')
171+ """
172+ )
173+
174+ result = pytester .runpytest_inprocess ("--tb=native" )
175+ assert result .ret == ExitCode .TESTS_FAILED
176+ failures = result .reprec .getfailures () # type: ignore[attr-defined]
177+ assert len (failures ) == 3
178+ lines1 = failures [1 ].longrepr .reprtraceback .reprentries [0 ].lines
179+ lines2 = failures [2 ].longrepr .reprtraceback .reprentries [0 ].lines
180+ assert len (lines1 ) == len (lines2 )
181+
145182
146183class BaseFunctionalTests :
147184 def test_passfunction (self , pytester : Pytester ) -> None :
0 commit comments