forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_discovery.py
More file actions
328 lines (287 loc) · 11.9 KB
/
test_discovery.py
File metadata and controls
328 lines (287 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import pathlib
import sys
from typing import Any, Dict, List
import pytest
from unittestadapter.discovery import discover_tests
from unittestadapter.pvsc_utils import TestNodeTypeEnum as NodeTypeEnum
from unittestadapter.pvsc_utils import parse_unittest_args
script_dir = pathlib.Path(__file__).parent.parent
sys.path.append(os.fspath(script_dir))
from tests.pytestadapter import helpers # noqa: E402
from tests.tree_comparison_helper import is_same_tree # noqa: E402
from . import expected_discovery_test_output # noqa: E402
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
@pytest.mark.parametrize(
("args", "expected"),
[
(
["-s", "something", "-p", "other*", "-t", "else"],
("something", "other*", "else", 1, None, None),
),
(
[
"--start-directory",
"foo",
"--pattern",
"bar*",
"--top-level-directory",
"baz",
],
("foo", "bar*", "baz", 1, None, None),
),
(
["--foo", "something"],
(".", "test*.py", None, 1, None, None),
),
(
["--foo", "something", "-v"],
(".", "test*.py", None, 2, None, None),
),
(
["--foo", "something", "-f"],
(".", "test*.py", None, 1, True, None),
),
(
["--foo", "something", "--verbose", "-f"],
(".", "test*.py", None, 2, True, None),
),
(
["--foo", "something", "-q", "--failfast"],
(".", "test*.py", None, 0, True, None),
),
(
["--foo", "something", "--quiet"],
(".", "test*.py", None, 0, None, None),
),
(
["--foo", "something", "--quiet", "--locals"],
(".", "test*.py", None, 0, None, True),
),
],
)
def test_parse_unittest_args(args: List[str], expected: List[str]) -> None:
"""The parse_unittest_args function should return values for the start_dir, pattern, and top_level_dir arguments when passed as command-line options, and ignore unrecognized arguments."""
actual = parse_unittest_args(args)
assert actual == expected
def test_simple_discovery() -> None:
"""The discover_tests function should return a dictionary with a "success" status, no errors, and a test tree if unittest discovery was performed successfully."""
start_dir = os.fsdecode(TEST_DATA_PATH)
pattern = "discovery_simple*"
file_path = os.fsdecode(pathlib.PurePath(TEST_DATA_PATH / "discovery_simple.py"))
expected = {
"path": start_dir,
"type_": NodeTypeEnum.folder,
"name": ".data",
"children": [
{
"name": "discovery_simple.py",
"type_": NodeTypeEnum.file,
"path": file_path,
"children": [
{
"name": "DiscoverySimple",
"path": file_path,
"type_": NodeTypeEnum.class_,
"children": [
{
"name": "test_one",
"path": file_path,
"type_": NodeTypeEnum.test,
"lineno": "14",
"id_": file_path + "\\" + "DiscoverySimple" + "\\" + "test_one",
},
{
"name": "test_two",
"path": file_path,
"type_": NodeTypeEnum.test,
"lineno": "17",
"id_": file_path + "\\" + "DiscoverySimple" + "\\" + "test_two",
},
],
"id_": file_path + "\\" + "DiscoverySimple",
}
],
"id_": file_path,
}
],
"id_": start_dir,
}
actual = discover_tests(start_dir, pattern, None)
assert actual["status"] == "success"
assert is_same_tree(actual.get("tests"), expected, ["id_", "lineno", "name"])
assert "error" not in actual
def test_simple_discovery_with_top_dir_calculated() -> None:
"""The discover_tests function should return a dictionary with a "success" status, no errors, and a test tree if unittest discovery was performed successfully."""
start_dir = "."
pattern = "discovery_simple*"
file_path = os.fsdecode(pathlib.PurePath(TEST_DATA_PATH / "discovery_simple.py"))
expected = {
"path": os.fsdecode(pathlib.PurePath(TEST_DATA_PATH)),
"type_": NodeTypeEnum.folder,
"name": ".data",
"children": [
{
"name": "discovery_simple.py",
"type_": NodeTypeEnum.file,
"path": file_path,
"children": [
{
"name": "DiscoverySimple",
"path": file_path,
"type_": NodeTypeEnum.class_,
"children": [
{
"name": "test_one",
"path": file_path,
"type_": NodeTypeEnum.test,
"lineno": "14",
"id_": file_path + "\\" + "DiscoverySimple" + "\\" + "test_one",
},
{
"name": "test_two",
"path": file_path,
"type_": NodeTypeEnum.test,
"lineno": "17",
"id_": file_path + "\\" + "DiscoverySimple" + "\\" + "test_two",
},
],
"id_": file_path + "\\" + "DiscoverySimple",
}
],
"id_": file_path,
}
],
"id_": os.fsdecode(pathlib.PurePath(TEST_DATA_PATH)),
}
# Define the CWD to be the root of the test data folder.
os.chdir(os.fsdecode(pathlib.PurePath(TEST_DATA_PATH)))
actual = discover_tests(start_dir, pattern, None)
assert actual["status"] == "success"
assert is_same_tree(actual.get("tests"), expected, ["id_", "lineno", "name"])
assert "error" not in actual
def test_empty_discovery() -> None:
"""The discover_tests function should return a dictionary with a "success" status, no errors, and no test tree if unittest discovery was performed successfully but no tests were found."""
start_dir = os.fsdecode(TEST_DATA_PATH)
pattern = "discovery_empty*"
actual = discover_tests(start_dir, pattern, None)
assert actual["status"] == "success"
assert "tests" in actual
assert "error" not in actual
def test_error_discovery() -> None:
"""The discover_tests function should return a dictionary with an "error" status, the discovered tests, and a list of errors if unittest discovery failed at some point."""
# Discover tests in .data/discovery_error/.
start_path = pathlib.PurePath(TEST_DATA_PATH / "discovery_error")
start_dir = os.fsdecode(start_path)
pattern = "file*"
file_path = os.fsdecode(start_path / "file_two.py")
expected = {
"path": start_dir,
"type_": NodeTypeEnum.folder,
"name": "discovery_error",
"children": [
{
"name": "file_two.py",
"type_": NodeTypeEnum.file,
"path": file_path,
"children": [
{
"name": "DiscoveryErrorTwo",
"path": file_path,
"type_": NodeTypeEnum.class_,
"children": [
{
"name": "test_one",
"path": file_path,
"type_": NodeTypeEnum.test,
"lineno": "14",
"id_": file_path + "\\" + "DiscoveryErrorTwo" + "\\" + "test_one",
},
{
"name": "test_two",
"path": file_path,
"type_": NodeTypeEnum.test,
"lineno": "17",
"id_": file_path + "\\" + "DiscoveryErrorTwo" + "\\" + "test_two",
},
],
"id_": file_path + "\\" + "DiscoveryErrorTwo",
}
],
"id_": file_path,
}
],
"id_": start_dir,
}
actual = discover_tests(start_dir, pattern, None)
assert actual["status"] == "error"
assert is_same_tree(expected, actual.get("tests"), ["id_", "lineno", "name"])
assert len(actual.get("error", [])) == 1
def test_unit_skip() -> None:
"""The discover_tests function should return a dictionary with a "success" status, no errors, and test tree.
if unittest discovery was performed and found a test in one file marked as skipped and another file marked as skipped.
"""
start_dir = os.fsdecode(TEST_DATA_PATH / "unittest_skip")
pattern = "unittest_*"
actual = discover_tests(start_dir, pattern, None)
assert actual["status"] == "success"
assert "tests" in actual
assert is_same_tree(
actual.get("tests"),
expected_discovery_test_output.skip_unittest_folder_discovery_output,
["id_", "lineno", "name"],
)
assert "error" not in actual
def test_complex_tree() -> None:
"""This test specifically tests when different start_dir and top_level_dir are provided."""
start_dir = os.fsdecode(
pathlib.PurePath(
TEST_DATA_PATH,
"utils_complex_tree",
"test_outer_folder",
"test_inner_folder",
)
)
pattern = "test_*.py"
top_level_dir = os.fsdecode(pathlib.PurePath(TEST_DATA_PATH, "utils_complex_tree"))
actual = discover_tests(start_dir, pattern, top_level_dir)
assert actual["status"] == "success"
assert "error" not in actual
assert is_same_tree(
actual.get("tests"),
expected_discovery_test_output.complex_tree_expected_output,
["id_", "lineno", "name"],
)
def test_simple_django_collect():
test_data_path: pathlib.Path = pathlib.Path(__file__).parent / ".data"
python_files_path: pathlib.Path = pathlib.Path(__file__).parent.parent.parent
discovery_script_path: str = os.fsdecode(python_files_path / "unittestadapter" / "discovery.py")
data_path: pathlib.Path = test_data_path / "simple_django"
manage_py_path: str = os.fsdecode(pathlib.Path(data_path, "manage.py"))
actual = helpers.runner_with_cwd_env(
[
discovery_script_path,
"--udiscovery",
],
data_path,
{"MANAGE_PY_PATH": manage_py_path},
)
assert actual
actual_list: List[Dict[str, Any]] = actual
assert actual_list is not None
if actual_list is not None:
actual_item = actual_list.pop(0)
assert all(item in actual_item for item in ("status", "cwd"))
assert actual_item.get("status") == "success", (
f"Status is not 'success', error is: {actual_item.get('error')}"
)
assert actual_item.get("cwd") == os.fspath(data_path)
assert len(actual_item["tests"]["children"]) == 1
assert actual_item["tests"]["children"][0]["children"][0]["id_"] == os.fsdecode(
pathlib.PurePath(test_data_path, "simple_django", "polls", "tests.py")
)
assert (
len(actual_item["tests"]["children"][0]["children"][0]["children"][0]["children"]) == 3
)