Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add collections errors to report #756

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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