@@ -100,6 +100,9 @@ def __init__(self, config: Config, file: Any = None) -> None:
100100 # Flag to suppress output during session start header
101101 self ._suppress_output = False
102102
103+ # Track collection errors separately (they occur during suppressed phase)
104+ self ._collection_errors : list [Any ] = []
105+
103106 # Wrap the terminal writer to intercept writes
104107 self ._original_tw = self ._tw
105108 self ._wrap_terminal_writer ()
@@ -225,6 +228,20 @@ def pytest_collection_finish(self, session: Any) -> None:
225228 # Re-enable output after collection (was suppressed since sessionstart)
226229 self ._suppress_output = False
227230
231+ def pytest_collectreport (self , report : Any ) -> None :
232+ """Track collection errors.
233+
234+ Collection happens while output is suppressed, so we capture errors
235+ here to display them later in the summary.
236+
237+ Args:
238+ report: collection report object
239+ """
240+ super ().pytest_collectreport (report )
241+ if report .failed :
242+ self ._total_errors += 1
243+ self ._collection_errors .append (report )
244+
228245 def pytest_runtest_logreport (self , report : TestReport ) -> None :
229246 """Process and format test results as they come in.
230247
@@ -726,6 +743,15 @@ def pytest_terminal_summary( # type: ignore[override]
726743 self ._print_file_results (self ._current_file )
727744 self ._current_file = None
728745
746+ # Print collection errors
747+ for report in self ._collection_errors :
748+ badge = "\033 [41m\033 [97m ERROR \033 [0m"
749+ self .write_line (f"\n { badge } { report .nodeid or 'collection error' } " , bold = True )
750+ longrepr_str = str (report .longrepr )
751+ for line in longrepr_str .split ("\n " ):
752+ self .write_line (f" { line } " )
753+ self .write_line ("" )
754+
729755 # Build summary line
730756 summary_parts = []
731757
@@ -744,6 +770,9 @@ def pytest_terminal_summary( # type: ignore[override]
744770 if self ._total_xpassed > 0 :
745771 summary_parts .append (f"{ self ._total_xpassed } xpassed" )
746772
773+ if self ._total_errors > 0 :
774+ summary_parts .append (f"{ self ._total_errors } error{ 's' if self ._total_errors > 1 else '' } " )
775+
747776 total_tests = (
748777 self ._total_passed
749778 + self ._total_failed
@@ -758,7 +787,7 @@ def pytest_terminal_summary( # type: ignore[override]
758787 summary_line = " Tests: " + ", " .join (summary_parts )
759788
760789 # Color the summary based on results
761- if self ._total_failed > 0 :
790+ if self ._total_failed > 0 or self . _total_errors > 0 :
762791 self .write_line (summary_line , red = True , bold = True )
763792 else :
764793 self .write_line (summary_line , green = True , bold = True )
0 commit comments