Skip to content

Commit 0b6fc5b

Browse files
anthonykim1Kim-Adeline Miguel
andauthored
Fix new unittest discovery issue (microsoft#19324)
* Add explicit type to errors list * First pass at test execution (Python-side) * How to swap to the new test adapter * fix new discovery multiclass tests detection * fix new discovery multiclass tests detection * fix duplicate ids for testing treeNodes * remove debugpy import * fixing test_simple_discovery * fix test_error_discovery, simple_tree * fix test_no_existing_child_node and test_get_existing_child_node * fix no_existing_child * fix lint error * fix no_existing str bad operation Co-authored-by: Kim-Adeline Miguel <kimiguel@microsoft.com>
1 parent 35b813f commit 0b6fc5b

10 files changed

Lines changed: 372 additions & 49 deletions

File tree

‎.vscode/launch.json‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@
235235
"name": "Node: Current File",
236236
"program": "${file}",
237237
"request": "launch",
238-
"skipFiles": [
239-
"<node_internals>/**"
240-
],
238+
"skipFiles": ["<node_internals>/**"],
241239
"type": "pwa-node"
242240
},
243241
{
@@ -248,6 +246,19 @@
248246
"program": "${file}",
249247
"console": "integratedTerminal",
250248
"cwd": "${workspaceFolder}"
249+
},
250+
{
251+
"name": "Listen",
252+
"type": "python",
253+
"request": "attach",
254+
"listen": { "host": "localhost", "port": 5678 },
255+
"justMyCode": true
256+
}
257+
],
258+
"compounds": [
259+
{
260+
"name": "Debug Test Discovery",
261+
"configurations": ["Listen", "Extension"]
251262
}
252263
]
253264
}

‎pythonFiles/testing_tools/adapter/__main__.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import sys
88

99
from . import pytest, report
10-
from .errors import UnsupportedToolError, UnsupportedCommandError
11-
10+
from .errors import UnsupportedCommandError, UnsupportedToolError
1211

1312
TOOLS = {
1413
"pytest": {

‎pythonFiles/tests/unittestadapter/test_discovery.py‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
import os
55
import pathlib
6+
from typing import List
67

78
import pytest
89
from unittestadapter.discovery import (
910
DEFAULT_PORT,
1011
discover_tests,
11-
parse_cli_args,
12-
parse_unittest_args,
12+
parse_discovery_cli_args,
13+
parse_unittest_discovery_args,
1314
)
1415
from unittestadapter.utils import TestNodeTypeEnum
1516

@@ -28,13 +29,13 @@
2829
),
2930
],
3031
)
31-
def test_parse_cli_args(args, expected) -> None:
32+
def test_parse_cli_args(args: List[str], expected: List[str]) -> None:
3233
"""The parse_cli_args function should parse and return the port and uuid passed as command-line options.
3334
3435
If there were no --port or --uuid command-line option, it should return default values).
3536
If there are multiple options, the last one wins.
3637
"""
37-
actual = parse_cli_args(args)
38+
actual = parse_discovery_cli_args(args)
3839

3940
assert expected == actual
4041

@@ -63,11 +64,11 @@ def test_parse_cli_args(args, expected) -> None:
6364
),
6465
],
6566
)
66-
def test_parse_unittest_args(args, expected) -> None:
67+
def test_parse_unittest_args(args: List[str], expected: List[str]) -> None:
6768
"""The parse_unittest_args function should return values for the start_dir, pattern, and top_level_dir arguments
6869
when passed as command-line options, and ignore unrecognized arguments.
6970
"""
70-
actual = parse_unittest_args(args)
71+
actual = parse_unittest_discovery_args(args)
7172

7273
assert actual == expected
7374

@@ -96,24 +97,35 @@ def test_simple_discovery() -> None:
9697
"type_": TestNodeTypeEnum.class_,
9798
"children": [
9899
{
99-
"id_": "discovery_simple.DiscoverySimple.test_one",
100100
"name": "test_one",
101101
"path": file_path,
102102
"type_": TestNodeTypeEnum.test,
103103
"lineno": "14",
104+
"id_": file_path
105+
+ "\\"
106+
+ "DiscoverySimple"
107+
+ "\\"
108+
+ "test_one",
104109
},
105110
{
106-
"id_": "discovery_simple.DiscoverySimple.test_two",
107111
"name": "test_two",
108112
"path": file_path,
109113
"type_": TestNodeTypeEnum.test,
110114
"lineno": "17",
115+
"id_": file_path
116+
+ "\\"
117+
+ "DiscoverySimple"
118+
+ "\\"
119+
+ "test_two",
111120
},
112121
],
122+
"id_": file_path + "\\" + "DiscoverySimple",
113123
}
114124
],
125+
"id_": file_path,
115126
}
116127
],
128+
"id_": start_dir,
117129
}
118130

119131
uuid = "some-uuid"
@@ -168,24 +180,35 @@ def test_error_discovery() -> None:
168180
"type_": TestNodeTypeEnum.class_,
169181
"children": [
170182
{
171-
"id_": "file_two.DiscoveryErrorTwo.test_one",
172183
"name": "test_one",
173184
"path": file_path,
174185
"type_": TestNodeTypeEnum.test,
175186
"lineno": "14",
187+
"id_": file_path
188+
+ "\\"
189+
+ "DiscoveryErrorTwo"
190+
+ "\\"
191+
+ "test_one",
176192
},
177193
{
178-
"id_": "file_two.DiscoveryErrorTwo.test_two",
179194
"name": "test_two",
180195
"path": file_path,
181196
"type_": TestNodeTypeEnum.test,
182197
"lineno": "17",
198+
"id_": file_path
199+
+ "\\"
200+
+ "DiscoveryErrorTwo"
201+
+ "\\"
202+
+ "test_two",
183203
},
184204
],
205+
"id_": file_path + "\\" + "DiscoveryErrorTwo",
185206
}
186207
],
208+
"id_": file_path,
187209
}
188210
],
211+
"id_": start_dir,
189212
}
190213

191214
uuid = "some-uuid"

‎pythonFiles/tests/unittestadapter/test_utils.py‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,27 @@ def test_get_existing_child_node() -> None:
7676
"path": "nested/one",
7777
"type_": TestNodeTypeEnum.folder,
7878
"children": [],
79+
"id_": "nested/one",
7980
},
8081
{
8182
"name": "nestedTwo",
8283
"path": "nested/two",
8384
"type_": TestNodeTypeEnum.folder,
8485
"children": [],
86+
"id_": "nested/two",
8587
},
8688
],
89+
"id_": "child/one",
8790
},
8891
{
8992
"name": "childTwo",
9093
"path": "child/two",
9194
"type_": TestNodeTypeEnum.folder,
9295
"children": [],
96+
"id_": "child/two",
9397
},
9498
],
99+
"id_": "foo",
95100
}
96101

97102
get_child_node("childTwo", "child/two", TestNodeTypeEnum.folder, tree)
@@ -119,22 +124,27 @@ def test_no_existing_child_node() -> None:
119124
"path": "nested/one",
120125
"type_": TestNodeTypeEnum.folder,
121126
"children": [],
127+
"id_": "nested/one",
122128
},
123129
{
124130
"name": "nestedTwo",
125131
"path": "nested/two",
126132
"type_": TestNodeTypeEnum.folder,
127133
"children": [],
134+
"id_": "nested/two",
128135
},
129136
],
137+
"id_": "child/one",
130138
},
131139
{
132140
"name": "childTwo",
133141
"path": "child/two",
134142
"type_": TestNodeTypeEnum.folder,
135143
"children": [],
144+
"id_": "child/two",
136145
},
137146
],
147+
"id_": "foo",
138148
}
139149

140150
# Make a separate copy of tree["children"].
@@ -180,24 +190,27 @@ def test_build_simple_tree() -> None:
180190
"type_": TestNodeTypeEnum.class_,
181191
"children": [
182192
{
183-
"id_": "utils_simple_tree.TreeOne.test_one",
184193
"name": "test_one",
185194
"path": file_path,
186195
"type_": TestNodeTypeEnum.test,
187196
"lineno": "13",
197+
"id_": file_path + "\\" + "TreeOne" + "\\" + "test_one",
188198
},
189199
{
190-
"id_": "utils_simple_tree.TreeOne.test_two",
191200
"name": "test_two",
192201
"path": file_path,
193202
"type_": TestNodeTypeEnum.test,
194203
"lineno": "16",
204+
"id_": file_path + "\\" + "TreeOne" + "\\" + "test_two",
195205
},
196206
],
207+
"id_": file_path + "\\" + "TreeOne",
197208
}
198209
],
210+
"id_": file_path,
199211
}
200212
],
213+
"id_": start_dir,
201214
}
202215

203216
loader = unittest.TestLoader()
@@ -235,24 +248,27 @@ def test_build_decorated_tree() -> None:
235248
"type_": TestNodeTypeEnum.class_,
236249
"children": [
237250
{
238-
"id_": "utils_decorated_tree.TreeOne.test_one",
239251
"name": "test_one",
240252
"path": file_path,
241253
"type_": TestNodeTypeEnum.test,
242254
"lineno": "24",
255+
"id_": file_path + "\\" + "TreeOne" + "\\" + "test_one",
243256
},
244257
{
245-
"id_": "utils_decorated_tree.TreeOne.test_two",
246258
"name": "test_two",
247259
"path": file_path,
248260
"type_": TestNodeTypeEnum.test,
249261
"lineno": "28",
262+
"id_": file_path + "\\" + "TreeOne" + "\\" + "test_two",
250263
},
251264
],
265+
"id_": file_path + "\\" + "TreeOne",
252266
}
253267
],
268+
"id_": file_path,
254269
}
255270
],
271+
"id_": start_dir,
256272
}
257273

258274
loader = unittest.TestLoader()

‎pythonFiles/unittestadapter/discovery.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import json
66
import os
7+
import pathlib
78
import sys
89
import traceback
910
import unittest
@@ -26,7 +27,7 @@
2627
DEFAULT_PORT = "45454"
2728

2829

29-
def parse_cli_args(args: List[str]) -> Tuple[int, Union[str, None]]:
30+
def parse_discovery_cli_args(args: List[str]) -> Tuple[int, Union[str, None]]:
3031
"""Parse command-line arguments that should be processed by the script.
3132
3233
So far this includes the port number that it needs to connect to, and the uuid passed by the TS side.
@@ -43,8 +44,8 @@ def parse_cli_args(args: List[str]) -> Tuple[int, Union[str, None]]:
4344
return int(parsed_args.port), parsed_args.uuid
4445

4546

46-
def parse_unittest_args(args: List[str]) -> Tuple[str, str, Union[str, None]]:
47-
"""Parse command-line arguments that should be forwarded to unittest.
47+
def parse_unittest_discovery_args(args: List[str]) -> Tuple[str, str, Union[str, None]]:
48+
"""Parse command-line arguments that should be forwarded to unittest to perform discovery.
4849
4950
Valid unittest arguments are: -v, -s, -p, -t and their long-form counterparts,
5051
however we only care about the last three.
@@ -113,13 +114,14 @@ def discover_tests(
113114
cwd = os.path.abspath(start_dir)
114115
payload: PayloadDict = {"cwd": cwd, "status": "success", "uuid": uuid}
115116
tests = None
116-
errors = []
117+
errors: List[str] = []
117118

118119
try:
119120
loader = unittest.TestLoader()
120121
suite = loader.discover(start_dir, pattern, top_level_dir)
121122

122-
tests, errors = build_test_tree(suite, cwd)
123+
tests, errors = build_test_tree(suite, cwd) # test tree built succesfully here.
124+
123125
except Exception:
124126
errors.append(traceback.format_exc())
125127

@@ -138,10 +140,10 @@ def discover_tests(
138140
argv = sys.argv[1:]
139141
index = argv.index("--udiscovery")
140142

141-
start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])
143+
start_dir, pattern, top_level_dir = parse_unittest_discovery_args(argv[index + 1 :])
142144

143145
# Perform test discovery.
144-
port, uuid = parse_cli_args(argv[:index])
146+
port, uuid = parse_discovery_cli_args(argv[:index])
145147
payload = discover_tests(start_dir, pattern, top_level_dir, uuid)
146148

147149
# Build the request data (it has to be a POST request or the Node side will not process it), and send it.

0 commit comments

Comments
 (0)