2
2
from pathlib import Path
3
3
from typing import Callable , Dict , List , Optional , Union
4
4
5
- from .base import NeotestAdapter , NeotestError , NeotestResult , NeotestResultStatus
6
-
7
5
import pytest
8
6
from _pytest ._code .code import ExceptionRepr
9
7
from _pytest .terminal import TerminalReporter
10
8
9
+ from .base import NeotestAdapter , NeotestError , NeotestResult , NeotestResultStatus
10
+
11
11
12
12
class PytestNeotestAdapter (NeotestAdapter ):
13
+ def __init__ (self , emit_parameterized_ids : bool ):
14
+ self .emit_parameterized_ids = emit_parameterized_ids
15
+
13
16
def run (
14
17
self ,
15
18
args : List [str ],
16
19
stream : Callable [[str , NeotestResult ], None ],
17
20
) -> Dict [str , NeotestResult ]:
18
- result_collector = NeotestResultCollector (self , stream = stream )
19
- pytest .main (args = args , plugins = [
20
- result_collector ,
21
- NeotestDebugpyPlugin (),
22
- ])
21
+ result_collector = NeotestResultCollector (
22
+ self , stream = stream , emit_parameterized_ids = self .emit_parameterized_ids
23
+ )
24
+ pytest .main (
25
+ args = args ,
26
+ plugins = [
27
+ result_collector ,
28
+ NeotestDebugpyPlugin (),
29
+ ],
30
+ )
23
31
return result_collector .results
24
32
25
33
@@ -28,9 +36,11 @@ def __init__(
28
36
self ,
29
37
adapter : PytestNeotestAdapter ,
30
38
stream : Callable [[str , NeotestResult ], None ],
39
+ emit_parameterized_ids : bool ,
31
40
):
32
41
self .stream = stream
33
42
self .adapter = adapter
43
+ self .emit_parameterized_ids = emit_parameterized_ids
34
44
35
45
self .pytest_config : Optional ["pytest.Config" ] = None # type: ignore
36
46
self .results : Dict [str , NeotestResult ] = {}
@@ -80,7 +90,9 @@ def pytest_cmdline_main(self, config: "pytest.Config"):
80
90
self .pytest_config = config
81
91
82
92
@pytest .hookimpl (hookwrapper = True )
83
- def pytest_runtest_makereport (self , item : "pytest.Item" , call : "pytest.CallInfo" ) -> None :
93
+ def pytest_runtest_makereport (
94
+ self , item : "pytest.Item" , call : "pytest.CallInfo"
95
+ ) -> None :
84
96
# pytest generates the report.outcome field in its internal
85
97
# pytest_runtest_makereport implementation, so call it first. (We don't
86
98
# implement pytest_runtest_logreport because it doesn't have access to
@@ -105,8 +117,10 @@ def pytest_runtest_makereport(self, item: "pytest.Item", call: "pytest.CallInfo"
105
117
msg_prefix = ""
106
118
if getattr (item , "callspec" , None ) is not None :
107
119
# Parametrized test
108
- msg_prefix = f"[{ item .callspec .id } ] "
109
- pos_id += f"[{ item .callspec .id } ]"
120
+ if self .emit_parameterized_ids :
121
+ pos_id += f"[{ item .callspec .id } ]"
122
+ else :
123
+ msg_prefix = f"[{ item .callspec .id } ] "
110
124
if report .outcome == "failed" :
111
125
exc_repr = report .longrepr
112
126
# Test fails due to condition outside of test e.g. xfail
@@ -119,7 +133,9 @@ def pytest_runtest_makereport(self, item: "pytest.Item", call: "pytest.CallInfo"
119
133
for traceback_entry in reversed (call .excinfo .traceback ):
120
134
if str (traceback_entry .path ) == abs_path :
121
135
error_line = traceback_entry .lineno
122
- errors .append ({"message" : msg_prefix + error_message , "line" : error_line })
136
+ errors .append (
137
+ {"message" : msg_prefix + error_message , "line" : error_line }
138
+ )
123
139
else :
124
140
# TODO: Figure out how these are returned and how to represent
125
141
raise Exception (
@@ -159,6 +175,7 @@ def maybe_debugpy_postmortem(excinfo):
159
175
"""
160
176
# Reference: https://github.com/microsoft/debugpy/issues/723
161
177
import threading
178
+
162
179
try :
163
180
import pydevd
164
181
except ImportError :
@@ -180,4 +197,4 @@ def maybe_debugpy_postmortem(excinfo):
180
197
181
198
182
199
def collect (args ):
183
- pytest .main ([' --collect-only' , '-q' ] + args )
200
+ pytest .main ([" --collect-only" , "-q" ] + args )
0 commit comments