|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| -from __future__ import print_function |
| 2 | + |
| 3 | +from __future__ import annotations |
3 | 4 |
|
4 | 5 | import os
|
5 | 6 | import sys
|
6 | 7 | from collections import OrderedDict
|
| 8 | +from typing import TYPE_CHECKING |
| 9 | + |
| 10 | +from _pytest._code.code import ExceptionRepr |
7 | 11 |
|
8 | 12 | import pytest
|
9 | 13 |
|
| 14 | +if TYPE_CHECKING: |
| 15 | + from _pytest.nodes import Item |
| 16 | + from _pytest.reports import CollectReport |
| 17 | + |
| 18 | + |
10 | 19 | # Reference:
|
11 | 20 | # https://docs.pytest.org/en/latest/writing_plugins.html#hookwrapper-executing-around-other-hooks
|
12 | 21 | # https://docs.pytest.org/en/latest/writing_plugins.html#hook-function-ordering-call-example
|
|
17 | 26 |
|
18 | 27 |
|
19 | 28 | @pytest.hookimpl(tryfirst=True, hookwrapper=True)
|
20 |
| -def pytest_runtest_makereport(item, call): |
| 29 | +def pytest_runtest_makereport(item: Item, call): |
21 | 30 | # execute all other hooks to obtain the report object
|
22 | 31 | outcome = yield
|
23 |
| - report = outcome.get_result() |
| 32 | + report: CollectReport = outcome.get_result() |
24 | 33 |
|
25 | 34 | # enable only in a workflow of GitHub Actions
|
26 | 35 | # ref: https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
|
@@ -57,13 +66,14 @@ def pytest_runtest_makereport(item, call):
|
57 | 66 | longrepr = report.head_line or item.name
|
58 | 67 |
|
59 | 68 | # get the error message and line number from the actual error
|
60 |
| - if hasattr(report.longrepr, "reprcrash"): |
61 |
| - longrepr += "\n\n" + report.longrepr.reprcrash.message |
62 |
| - traceback_entries = report.longrepr.reprtraceback.reprentries |
63 |
| - if len(traceback_entries) > 1: |
| 69 | + if isinstance(report.longrepr, ExceptionRepr): |
| 70 | + if report.longrepr.reprcrash is not None: |
| 71 | + longrepr += "\n\n" + report.longrepr.reprcrash.message |
| 72 | + tb_entries = report.longrepr.reprtraceback.reprentries |
| 73 | + if len(tb_entries) > 1 and tb_entries[0].reprfileloc is not None: |
64 | 74 | # Handle third-party exceptions
|
65 |
| - lineno = traceback_entries[0].reprfileloc.lineno |
66 |
| - else: |
| 75 | + lineno = tb_entries[0].reprfileloc.lineno |
| 76 | + elif report.longrepr.reprcrash is not None: |
67 | 77 | lineno = report.longrepr.reprcrash.lineno
|
68 | 78 | elif isinstance(report.longrepr, tuple):
|
69 | 79 | _, lineno, message = report.longrepr
|
|
0 commit comments