forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_discovery.py
More file actions
252 lines (226 loc) · 9.28 KB
/
Copy pathtest_discovery.py
File metadata and controls
252 lines (226 loc) · 9.28 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import shutil
from typing import Any, Dict, List, Optional
import pytest
from . import expected_discovery_test_output
from .helpers import TEST_DATA_PATH, runner, runner_with_cwd
def test_import_error(tmp_path):
"""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.
"""
# 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 = TEST_DATA_PATH / "error_pytest_import.txt"
temp_dir = tmp_path / "temp_data"
temp_dir.mkdir()
p = temp_dir / "error_pytest_import.py"
shutil.copyfile(file_path, p)
actual: Optional[List[Dict[str, Any]]] = runner(["--collect-only", os.fspath(p)])
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
assert actual_list.pop(-1).get("eot")
for actual_item in actual_list:
assert all(
item in actual_item.keys() for item in ("status", "cwd", "error")
)
assert actual_item.get("status") == "error"
assert actual_item.get("cwd") == os.fspath(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:
assert False
def test_syntax_error(tmp_path):
"""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 = TEST_DATA_PATH / "error_syntax_discovery.txt"
temp_dir = tmp_path / "temp_data"
temp_dir.mkdir()
p = temp_dir / "error_syntax_discovery.py"
shutil.copyfile(file_path, p)
actual = runner(["--collect-only", os.fspath(p)])
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
assert actual_list.pop(-1).get("eot")
for actual_item in actual_list:
assert all(
item in actual_item.keys() for item in ("status", "cwd", "error")
)
assert actual_item.get("status") == "error"
assert actual_item.get("cwd") == os.fspath(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:
assert False
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 = runner(["--collect-only", file_path_str])
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
assert actual_list.pop(-1).get("eot")
for actual_item in actual_list:
assert all(
item in actual_item.keys() for item in ("status", "cwd", "error")
)
assert actual_item.get("status") == "error"
assert actual_item.get("cwd") == os.fspath(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:
assert False
@pytest.mark.parametrize(
"file, expected_const",
[
(
"test_multi_class_nest.py",
expected_discovery_test_output.nested_classes_expected_test_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,
),
],
)
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 = runner(
[
"--collect-only",
os.fspath(TEST_DATA_PATH / file),
]
)
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
assert actual_list.pop(-1).get("eot")
actual_item = actual_list.pop(0)
assert all(item in actual_item.keys() for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "success"
assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH)
assert actual_item.get("tests") == expected_const
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={TEST_DATA_PATH / 'root' / 'tests'}"
actual = runner_with_cwd(
[
"--collect-only",
rd,
],
TEST_DATA_PATH / "root",
)
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
assert actual_list.pop(-1).get("eot")
actual_item = actual_list.pop(0)
assert all(item in actual_item.keys() for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "success"
assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH / "root")
assert (
actual_item.get("tests")
== expected_discovery_test_output.root_with_config_expected_output
)
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 = runner_with_cwd(
[
"--collect-only",
"-c",
"tests/pytest.ini",
],
TEST_DATA_PATH / "root",
)
assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
assert actual_list.pop(-1).get("eot")
actual_item = actual_list.pop(0)
assert all(item in actual_item.keys() for item in ("status", "cwd", "error"))
assert actual_item.get("status") == "success"
assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH / "root")
assert (
actual_item.get("tests")
== expected_discovery_test_output.root_with_config_expected_output
)