Skip to content

Commit fa38f28

Browse files
authored
add a separate code path for test collection errors (#10)
1 parent 1bf68e1 commit fa38f28

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

parse_logs.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import more_itertools
1212
from pytest import CollectReport, TestReport
1313

14+
test_collection_stage = "test collection session"
15+
1416

1517
@dataclass
1618
class SessionStart:
@@ -44,6 +46,12 @@ class PreformattedReport:
4446
message: str
4547

4648

49+
@dataclass
50+
class CollectionError:
51+
name: str
52+
repr_: str
53+
54+
4755
def parse_record(record):
4856
report_types = {
4957
"TestReport": TestReport,
@@ -84,6 +92,9 @@ def _(report: TestReport):
8492

8593
@preformat_report.register
8694
def _(report: CollectReport):
95+
if report.nodeid == "":
96+
return CollectionError(name=test_collection_stage, repr_=str(report.longrepr))
97+
8798
parsed = parse_nodeid(report.nodeid)
8899
message = report.longrepr.split("\n")[-1].removeprefix("E").lstrip()
89100
return PreformattedReport(message=message, **parsed)
@@ -172,6 +183,21 @@ def compressed_report(reports, max_chars, **formatter_kwargs):
172183
return summarize(reports, **formatter_kwargs)
173184

174185

186+
def format_collection_error(error, **formatter_kwargs):
187+
return textwrap.dedent(
188+
"""\
189+
<details><summary>Python {py_version} Test Summary</summary>
190+
191+
{name} failed:
192+
```
193+
{traceback}
194+
```
195+
196+
</details>
197+
"""
198+
).format(py_version=py_version, name=error.name, traceback=error.repr_)
199+
200+
175201
if __name__ == "__main__":
176202
parser = argparse.ArgumentParser()
177203
parser.add_argument("filepath", type=pathlib.Path)
@@ -186,8 +212,12 @@ def compressed_report(reports, max_chars, **formatter_kwargs):
186212

187213
failed = [report for report in reports if report.outcome == "failed"]
188214
preformatted = [preformat_report(report) for report in failed]
189-
190-
message = compressed_report(preformatted, max_chars=65535, py_version=py_version)
215+
if len(preformatted) == 1 and isinstance(preformatted[0], CollectionError):
216+
message = format_collection_error(preformatted[0], py_version=py_version)
217+
else:
218+
message = compressed_report(
219+
preformatted, max_chars=65535, py_version=py_version
220+
)
191221

192222
output_file = pathlib.Path("pytest-logs.txt")
193223
print(f"Writing output file to: {output_file.absolute()}")

0 commit comments

Comments
 (0)