forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtest_discovery.py
366 lines (320 loc) · 14.6 KB
/
test_discovery.py
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
import os
import sys
from typing import Any, Dict, List, Optional
import pytest
from tests.tree_comparison_helper import is_same_tree
from . import expected_discovery_test_output, helpers
def test_import_error():
"""Test pytest discovery on a file that has a pytest marker but does not import pytest.
Copies the contents of a .txt file to a .py file in the temporary directory
to then run pytest discovery on.
The json should still be returned but the errors list should be present.
Keyword arguments:
tmp_path -- pytest fixture that creates a temporary directory.
"""
file_path = helpers.TEST_DATA_PATH / "error_pytest_import.txt"
with helpers.text_to_python_file(file_path) as p:
actual: Optional[List[Dict[str, Any]]] = helpers.runner(["--collect-only", os.fspath(p)])
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
for actual_item in actual_list:
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "error"
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH)
# Ensure that 'error' is a list and then check its length
error_content = actual_item.get("error")
if error_content is not None and isinstance(
error_content, (list, tuple, str)
): # You can add other types if needed
assert len(error_content) == 2
else:
pytest.fail(f"{error_content} is None or not a list, str, or tuple")
def test_syntax_error(tmp_path): # noqa: ARG001
"""Test pytest discovery on a file that has a syntax error.
Copies the contents of a .txt file to a .py file in the temporary directory
to then run pytest discovery on.
The json should still be returned but the errors list should be present.
Keyword arguments:
tmp_path -- pytest fixture that creates a temporary directory.
"""
# Saving some files as .txt to avoid that file displaying a syntax error for
# the extension as a whole. Instead, rename it before running this test
# in order to test the error handling.
file_path = helpers.TEST_DATA_PATH / "error_syntax_discovery.txt"
with helpers.text_to_python_file(file_path) as p:
actual = helpers.runner(["--collect-only", os.fspath(p)])
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
for actual_item in actual_list:
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "error"
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH)
# Ensure that 'error' is a list and then check its length
error_content = actual_item.get("error")
if error_content is not None and isinstance(
error_content, (list, tuple, str)
): # You can add other types if needed
assert len(error_content) == 2
else:
pytest.fail(f"{error_content} is None or not a list, str, or tuple")
def test_parameterized_error_collect():
"""Tests pytest discovery on specific file that incorrectly uses parametrize.
The json should still be returned but the errors list should be present.
"""
file_path_str = "error_parametrize_discovery.py"
actual = helpers.runner(["--collect-only", file_path_str])
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
for actual_item in actual_list:
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "error"
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH)
# Ensure that 'error' is a list and then check its length
error_content = actual_item.get("error")
if error_content is not None and isinstance(
error_content, (list, tuple, str)
): # You can add other types if needed
assert len(error_content) == 2
else:
pytest.fail(f"{error_content} is None or not a list, str, or tuple")
@pytest.mark.parametrize(
("file", "expected_const"),
[
(
"test_param_span_class.py",
expected_discovery_test_output.test_param_span_class_expected_output,
),
(
"test_multi_class_nest.py",
expected_discovery_test_output.nested_classes_expected_test_output,
),
(
"same_function_new_class_param.py",
expected_discovery_test_output.same_function_new_class_param_expected_output,
),
(
"unittest_skiptest_file_level.py",
expected_discovery_test_output.unittest_skip_file_level_expected_output,
),
(
"param_same_name",
expected_discovery_test_output.param_same_name_expected_output,
),
(
"parametrize_tests.py",
expected_discovery_test_output.parametrize_tests_expected_output,
),
(
"empty_discovery.py",
expected_discovery_test_output.empty_discovery_pytest_expected_output,
),
(
"simple_pytest.py",
expected_discovery_test_output.simple_discovery_pytest_expected_output,
),
(
"unittest_pytest_same_file.py",
expected_discovery_test_output.unit_pytest_same_file_discovery_expected_output,
),
(
"unittest_folder",
expected_discovery_test_output.unittest_folder_discovery_expected_output,
),
(
"dual_level_nested_folder",
expected_discovery_test_output.dual_level_nested_folder_expected_output,
),
(
"folder_a",
expected_discovery_test_output.double_nested_folder_expected_output,
),
(
"text_docstring.txt",
expected_discovery_test_output.doctest_pytest_expected_output,
),
(
"pytest_describe_plugin" + os.path.sep + "describe_only.py",
expected_discovery_test_output.expected_describe_only_output,
),
(
"pytest_describe_plugin" + os.path.sep + "nested_describe.py",
expected_discovery_test_output.expected_nested_describe_output,
),
],
)
def test_pytest_collect(file, expected_const):
"""Test to test pytest discovery on a variety of test files/ folder structures.
Uses variables from expected_discovery_test_output.py to store the expected
dictionary return. Only handles discovery and therefore already contains the arg
--collect-only. All test discovery will succeed, be in the correct cwd, and match
expected test output.
Keyword arguments:
file -- a string with the file or folder to run pytest discovery on.
expected_const -- the expected output from running pytest discovery on the file.
"""
actual = helpers.runner(
[
os.fspath(helpers.TEST_DATA_PATH / file),
"--collect-only",
]
)
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
actual_item = actual_list.pop(0)
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "success", (
f"Status is not 'success', error is: {actual_item.get('error')}"
)
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH)
assert is_same_tree(
actual_item.get("tests"),
expected_const,
["id_", "lineno", "name", "runID"],
), (
f"Tests tree does not match expected value. \n Expected: {json.dumps(expected_const, indent=4)}. \n Actual: {json.dumps(actual_item.get('tests'), indent=4)}"
)
@pytest.mark.skipif(
sys.platform == "win32",
reason="See https://stackoverflow.com/questions/32877260/privlege-error-trying-to-create-symlink-using-python-on-windows-10",
)
def test_symlink_root_dir():
"""Test to test pytest discovery with the command line arg --rootdir specified as a symlink path.
Discovery should succeed and testids should be relative to the symlinked root directory.
"""
with helpers.create_symlink(helpers.TEST_DATA_PATH, "root", "symlink_folder") as (
source,
destination,
):
assert destination.is_symlink()
# Run pytest with the cwd being the resolved symlink path (as it will be when we run the subprocess from node).
actual = helpers.runner_with_cwd(
["--collect-only", f"--rootdir={os.fspath(destination)}"], source
)
expected = expected_discovery_test_output.symlink_expected_discovery_output
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
actual_item = actual_list.pop(0)
try:
# Check if all requirements
assert all(item in actual_item for item in ("status", "cwd", "error")), (
"Required keys are missing"
)
assert actual_item.get("status") == "success", "Status is not 'success'"
assert actual_item.get("cwd") == os.fspath(destination), (
f"CWD does not match: {os.fspath(destination)}"
)
assert actual_item.get("tests") == expected, "Tests do not match expected value"
except AssertionError as e:
# Print the actual_item in JSON format if an assertion fails
print(json.dumps(actual_item, indent=4))
pytest.fail(str(e))
def test_pytest_root_dir():
"""Test to test pytest discovery with the command line arg --rootdir specified to be a subfolder of the workspace root.
Discovery should succeed and testids should be relative to workspace root.
"""
rd = f"--rootdir={helpers.TEST_DATA_PATH / 'root' / 'tests'}"
actual = helpers.runner_with_cwd(
[
"--collect-only",
rd,
],
helpers.TEST_DATA_PATH / "root",
)
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
actual_item = actual_list.pop(0)
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "success"
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH / "root")
assert is_same_tree(
actual_item.get("tests"),
expected_discovery_test_output.root_with_config_expected_output,
["id_", "lineno", "name", "runID"],
), (
f"Tests tree does not match expected value. \n Expected: {json.dumps(expected_discovery_test_output.root_with_config_expected_output, indent=4)}. \n Actual: {json.dumps(actual_item.get('tests'), indent=4)}"
)
def test_pytest_config_file():
"""Test to test pytest discovery with the command line arg -c with a specified config file which changes the workspace root.
Discovery should succeed and testids should be relative to workspace root.
"""
actual = helpers.runner_with_cwd(
[
"--collect-only",
"tests/",
],
helpers.TEST_DATA_PATH / "root",
)
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
actual_item = actual_list.pop(0)
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "success"
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH / "root")
assert is_same_tree(
actual_item.get("tests"),
expected_discovery_test_output.root_with_config_expected_output,
["id_", "lineno", "name", "runID"],
), (
f"Tests tree does not match expected value. \n Expected: {json.dumps(expected_discovery_test_output.root_with_config_expected_output, indent=4)}. \n Actual: {json.dumps(actual_item.get('tests'), indent=4)}"
)
def test_config_sub_folder():
"""Here the session node will be a subfolder of the workspace root and the test are in another subfolder.
This tests checks to see if test node path are under the session node and if so the
session node is correctly updated to the common path.
"""
folder_path = helpers.TEST_DATA_PATH / "config_sub_folder"
actual = helpers.runner_with_cwd(
[
"--collect-only",
"-c=config/pytest.ini",
"--rootdir=config/",
"-vv",
],
folder_path,
)
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
actual_item = actual_list.pop(0)
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "success"
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH / "config_sub_folder")
assert actual_item.get("tests") is not None
if actual_item.get("tests") is not None:
tests: Any = actual_item.get("tests")
assert tests.get("name") == "config_sub_folder"
def test_ruff_plugin():
"""Here the session node will be a subfolder of the workspace root and the test are in another subfolder.
This tests checks to see if test node path are under the session node and if so the
session node is correctly updated to the common path.
"""
file_path = helpers.TEST_DATA_PATH / "folder_with_script"
actual = helpers.runner(
[os.fspath(file_path), "--collect-only", "--ruff"],
)
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
actual_item = actual_list.pop(0)
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "success", (
f"Status is not 'success', error is: {actual_item.get('error')}"
)
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH)
assert is_same_tree(
actual_item.get("tests"),
expected_discovery_test_output.ruff_test_expected_output,
["id_", "lineno", "name", "runID"],
), (
f"Tests tree does not match expected value. \n Expected: {json.dumps(expected_discovery_test_output.ruff_test_expected_output, indent=4)}. \n Actual: {json.dumps(actual_item.get('tests'), indent=4)}"
)