forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_execution.py
More file actions
284 lines (259 loc) · 9.32 KB
/
Copy pathtest_execution.py
File metadata and controls
284 lines (259 loc) · 9.32 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import pathlib
import sys
from typing import List
import pytest
PYTHON_FILES = pathlib.Path(__file__).parent.parent
sys.path.insert(0, os.fspath(PYTHON_FILES))
from unittestadapter.execution import parse_execution_cli_args, run_tests
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
@pytest.mark.parametrize(
"args, expected",
[
(
[
"--port",
"111",
"--uuid",
"fake-uuid",
],
(111, "fake-uuid"),
),
(
["--port", "111", "--uuid", "fake-uuid"],
(111, "fake-uuid"),
),
(
[
"--port",
"111",
"--uuid",
"fake-uuid",
"-v",
"-s",
],
(111, "fake-uuid"),
),
],
)
def test_parse_execution_cli_args(args: List[str], expected: List[str]) -> None:
"""The parse_execution_cli_args function should return values for the port, uuid, and testids arguments
when passed as command-line options, and ignore unrecognized arguments.
"""
actual = parse_execution_cli_args(args)
assert actual == expected
def test_no_ids_run() -> None:
"""This test runs on an empty array of test_ids, therefore it should return
an empty dict for the result.
"""
start_dir: str = os.fspath(TEST_DATA_PATH)
testids = []
pattern = "discovery_simple*"
actual = run_tests(start_dir, testids, pattern, None, "fake-uuid")
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
if actual["result"] is not None:
assert len(actual["result"]) == 0
else:
raise AssertionError("actual['result'] is None")
def test_single_ids_run() -> None:
"""This test runs on a single test_id, therefore it should return
a dict with a single key-value pair for the result.
This single test passes so the outcome should be 'success'.
"""
id = "discovery_simple.DiscoverySimple.test_one"
actual = run_tests(
os.fspath(TEST_DATA_PATH), [id], "discovery_simple*", None, "fake-uuid"
)
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert actual["result"] is not None
result = actual["result"]
assert len(result) == 1
assert id in result
id_result = result[id]
assert id_result is not None
assert "outcome" in id_result
assert id_result["outcome"] == "success"
def test_subtest_run() -> None:
"""This test runs on a the test_subtest which has a single method, test_even,
that uses unittest subtest.
The actual result of run should return a dict payload with 6 entry for the 6 subtests.
"""
id = "test_subtest.NumbersTest.test_even"
actual = run_tests(
os.fspath(TEST_DATA_PATH), [id], "test_subtest.py", None, "fake-uuid"
)
subtests_ids = [
"test_subtest.NumbersTest.test_even (i=0)",
"test_subtest.NumbersTest.test_even (i=1)",
"test_subtest.NumbersTest.test_even (i=2)",
"test_subtest.NumbersTest.test_even (i=3)",
"test_subtest.NumbersTest.test_even (i=4)",
"test_subtest.NumbersTest.test_even (i=5)",
]
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert actual["result"] is not None
result = actual["result"]
assert len(result) == 6
for id in subtests_ids:
assert id in result
@pytest.mark.parametrize(
"test_ids, pattern, cwd, expected_outcome",
[
(
[
"test_add.TestAddFunction.test_add_negative_numbers",
"test_add.TestAddFunction.test_add_positive_numbers",
],
"test_add.py",
os.fspath(TEST_DATA_PATH / "unittest_folder"),
"success",
),
(
[
"test_add.TestAddFunction.test_add_negative_numbers",
"test_add.TestAddFunction.test_add_positive_numbers",
"test_subtract.TestSubtractFunction.test_subtract_negative_numbers",
"test_subtract.TestSubtractFunction.test_subtract_positive_numbers",
],
"test*",
os.fspath(TEST_DATA_PATH / "unittest_folder"),
"success",
),
(
[
"pattern_a_test.DiscoveryA.test_one_a",
"pattern_a_test.DiscoveryA.test_two_a",
],
"*test",
os.fspath(TEST_DATA_PATH / "two_patterns"),
"success",
),
(
[
"test_pattern_b.DiscoveryB.test_one_b",
"test_pattern_b.DiscoveryB.test_two_b",
],
"test_*",
os.fspath(TEST_DATA_PATH / "two_patterns"),
"success",
),
(
[
"file_one.CaseTwoFileOne.test_one",
"file_one.CaseTwoFileOne.test_two",
"folder.file_two.CaseTwoFileTwo.test_one",
"folder.file_two.CaseTwoFileTwo.test_two",
],
"*",
os.fspath(TEST_DATA_PATH / "utils_nested_cases"),
"success",
),
(
[
"test_two_classes.ClassOne.test_one",
"test_two_classes.ClassTwo.test_two",
],
"test_two_classes.py",
os.fspath(TEST_DATA_PATH),
"success",
),
],
)
def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
"""
The following are all successful tests of different formats.
# 1. Two tests with the `pattern` specified as a file
# 2. Two test files in the same folder called `unittest_folder`
# 3. A folder with two different test file patterns, this test gathers pattern `*test`
# 4. A folder with two different test file patterns, this test gathers pattern `test_*`
# 5. A nested structure where a test file is on the same level as a folder containing a test file
# 6. Test file with two test classes
All tests should have the outcome of `success`.
"""
actual = run_tests(cwd, test_ids, pattern, None, "fake-uuid")
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
assert actual["cwd"] == cwd
assert actual["result"] is not None
result = actual["result"]
assert len(result) == len(test_ids)
for test_id in test_ids:
assert test_id in result
id_result = result[test_id]
assert id_result is not None
assert "outcome" in id_result
assert id_result["outcome"] == expected_outcome
assert True
def test_failed_tests():
"""This test runs on a single file `test_fail` with two tests that fail."""
test_ids = [
"test_fail_simple.RunFailSimple.test_one_fail",
"test_fail_simple.RunFailSimple.test_two_fail",
]
actual = run_tests(
os.fspath(TEST_DATA_PATH), test_ids, "test_fail_simple*", None, "fake-uuid"
)
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert actual["result"] is not None
result = actual["result"]
assert len(result) == len(test_ids)
for test_id in test_ids:
assert test_id in result
id_result = result[test_id]
assert id_result is not None
assert "outcome" in id_result
assert id_result["outcome"] == "failure"
assert "message" and "traceback" in id_result
assert True
def test_unknown_id():
"""This test runs on a unknown test_id, therefore it should return
an error as the outcome as it attempts to find the given test.
"""
test_ids = ["unknown_id"]
actual = run_tests(
os.fspath(TEST_DATA_PATH), test_ids, "test_fail_simple*", None, "fake-uuid"
)
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert actual["result"] is not None
result = actual["result"]
assert len(result) == len(test_ids)
assert "unittest.loader._FailedTest.unknown_id" in result
id_result = result["unittest.loader._FailedTest.unknown_id"]
assert id_result is not None
assert "outcome" in id_result
assert id_result["outcome"] == "error"
assert "message" and "traceback" in id_result
def test_incorrect_path():
"""This test runs on a non existent path, therefore it should return
an error as the outcome as it attempts to find the given folder.
"""
test_ids = ["unknown_id"]
actual = run_tests(
os.fspath(TEST_DATA_PATH / "unknown_folder"),
test_ids,
"test_fail_simple*",
None,
"fake-uuid",
)
assert actual
assert all(item in actual for item in ("cwd", "status", "error"))
assert actual["status"] == "error"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH / "unknown_folder")