diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 74aba66c..5a896165 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -192,6 +192,11 @@ def pytest_terminal_summary(self, terminalreporter): f"Generated html report: {self._report_path.as_uri()}", ) + @pytest.hookimpl(trylast=True) + def pytest_collectreport(self, report): + if report.failed: + self._process_report(report, 0) + @pytest.hookimpl(trylast=True) def pytest_collection_finish(self, session): self._report.collected_items = len(session.items) @@ -299,7 +304,9 @@ def _format_duration(duration): def _is_error(report): - return report.when in ["setup", "teardown"] and report.outcome == "failed" + return ( + report.when in ["setup", "teardown", "collect"] and report.outcome == "failed" + ) def _process_logs(report): diff --git a/src/pytest_html/report_data.py b/src/pytest_html/report_data.py index b3b9bde5..7be80531 100644 --- a/src/pytest_html/report_data.py +++ b/src/pytest_html/report_data.py @@ -133,7 +133,7 @@ def add_test(self, test_data, report, outcome, logs): self.append_teardown_log(report) # passed "setup" and "teardown" are not added to the html - if report.when == "call" or ( + if report.when in ["call", "collect"] or ( report.when in ["setup", "teardown"] and report.outcome != "passed" ): test_data["log"] = _handle_ansi("\n".join(logs)) diff --git a/testing/test_integration.py b/testing/test_integration.py index 39b46bf0..cfe80336 100644 --- a/testing/test_integration.py +++ b/testing/test_integration.py @@ -758,6 +758,20 @@ def test_streams(setup): assert_that(log).matches(f"- Captured {stream} {when} -") assert_that(log).matches(f"this is {when} {stream}") + def test_collect_error(self, pytester): + error_msg = "Non existent module" + pytester.makepyfile( + f""" + import pytest + raise ImportError("{error_msg}") + """ + ) + page = run(pytester) + assert_results(page, error=1) + + log = get_log(page) + assert_that(log).matches(rf"E\s+ImportError: {error_msg}") + class TestLogCapturing: LOG_LINE_REGEX = r"\s+this is {}"