Skip to content

Commit

Permalink
fix: Add collections errors to report (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil authored Nov 1, 2023
1 parent bf19498 commit cbb3b39
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_html/report_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions testing/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}"
Expand Down

0 comments on commit cbb3b39

Please sign in to comment.