Skip to content

Commit e95bfbd

Browse files
committed
fix: Add collections errors to report
1 parent bf19498 commit e95bfbd

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/pytest_html/basereport.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ def pytest_terminal_summary(self, terminalreporter):
192192
f"Generated html report: {self._report_path.as_uri()}",
193193
)
194194

195+
@pytest.hookimpl(trylast=True)
196+
def pytest_collectreport(self, report):
197+
if report.failed:
198+
self._process_report(report, 0)
199+
195200
@pytest.hookimpl(trylast=True)
196201
def pytest_collection_finish(self, session):
197202
self._report.collected_items = len(session.items)
@@ -299,7 +304,9 @@ def _format_duration(duration):
299304

300305

301306
def _is_error(report):
302-
return report.when in ["setup", "teardown"] and report.outcome == "failed"
307+
return (report.when in ["setup", "teardown"] and report.outcome == "failed") or (
308+
report.when == "collect" and report.outcome == "failed"
309+
)
303310

304311

305312
def _process_logs(report):

src/pytest_html/report_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def add_test(self, test_data, report, outcome, logs):
133133
self.append_teardown_log(report)
134134

135135
# passed "setup" and "teardown" are not added to the html
136-
if report.when == "call" or (
136+
if report.when in ["call", "collect"] or (
137137
report.when in ["setup", "teardown"] and report.outcome != "passed"
138138
):
139139
test_data["log"] = _handle_ansi("\n".join(logs))

testing/test_integration.py

+14
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,20 @@ def test_streams(setup):
758758
assert_that(log).matches(f"- Captured {stream} {when} -")
759759
assert_that(log).matches(f"this is {when} {stream}")
760760

761+
def test_collect_error(self, pytester):
762+
error_msg = "Non existent module"
763+
pytester.makepyfile(
764+
f"""
765+
import pytest
766+
raise ImportError("{error_msg}")
767+
"""
768+
)
769+
page = run(pytester)
770+
assert_results(page, error=1)
771+
772+
log = get_log(page)
773+
assert_that(log).matches(rf"E\s+ImportError: {error_msg}")
774+
761775

762776
class TestLogCapturing:
763777
LOG_LINE_REGEX = r"\s+this is {}"

0 commit comments

Comments
 (0)