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
197 lines (172 loc) · 6.5 KB
/
Copy pathtest_discovery.py
File metadata and controls
197 lines (172 loc) · 6.5 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import pathlib
import pytest
from unittestadapter.discovery import (
DEFAULT_PORT,
discover_tests,
parse_cli_args,
parse_unittest_args,
)
from unittestadapter.utils import TestNodeTypeEnum
from .helpers import TEST_DATA_PATH, is_same_tree
@pytest.mark.parametrize(
"args, expected",
[
(["--port", "6767", "--uuid", "some-uuid"], (6767, "some-uuid")),
(["--foo", "something", "--bar", "another"], (int(DEFAULT_PORT), None)),
(["--port", "4444", "--foo", "something", "--port", "9999"], (9999, None)),
(
["--uuid", "first-uuid", "--bar", "other", "--uuid", "second-uuid"],
(int(DEFAULT_PORT), "second-uuid"),
),
],
)
def test_parse_cli_args(args, expected) -> None:
"""The parse_cli_args function should parse and return the port and uuid passed as command-line options.
If there were no --port or --uuid command-line option, it should return default values).
If there are multiple options, the last one wins.
"""
actual = parse_cli_args(args)
assert expected == actual
@pytest.mark.parametrize(
"args, expected",
[
(
["-s", "something", "-p", "other*", "-t", "else"],
("something", "other*", "else"),
),
(
[
"--start-directory",
"foo",
"--pattern",
"bar*",
"--top-level-directory",
"baz",
],
("foo", "bar*", "baz"),
),
(
["--foo", "something"],
(".", "test*.py", None),
),
],
)
def test_parse_unittest_args(args, expected) -> 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, a uuid, 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_": TestNodeTypeEnum.folder,
"name": ".data",
"children": [
{
"name": "discovery_simple.py",
"type_": TestNodeTypeEnum.file,
"path": file_path,
"children": [
{
"name": "DiscoverySimple",
"path": file_path,
"type_": TestNodeTypeEnum.class_,
"children": [
{
"id_": "discovery_simple.DiscoverySimple.test_one",
"name": "test_one",
"path": file_path,
"type_": TestNodeTypeEnum.test,
"lineno": "14",
},
{
"id_": "discovery_simple.DiscoverySimple.test_two",
"name": "test_two",
"path": file_path,
"type_": TestNodeTypeEnum.test,
"lineno": "17",
},
],
}
],
}
],
}
uuid = "some-uuid"
actual = discover_tests(start_dir, pattern, None, uuid)
assert actual["status"] == "success"
assert actual["uuid"] == uuid
assert is_same_tree(actual.get("tests"), expected)
assert "errors" not in actual
def test_empty_discovery() -> None:
"""The discover_tests function should return a dictionary with a "success" status, a uuid, 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*"
uuid = "some-uuid"
actual = discover_tests(start_dir, pattern, None, uuid)
assert actual["status"] == "success"
assert actual["uuid"] == uuid
assert "tests" not in actual
assert "errors" not in actual
def test_error_discovery() -> None:
"""The discover_tests function should return a dictionary with an "error" status, a uuid, 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_": TestNodeTypeEnum.folder,
"name": "discovery_error",
"children": [
{
"name": "file_two.py",
"type_": TestNodeTypeEnum.file,
"path": file_path,
"children": [
{
"name": "DiscoveryErrorTwo",
"path": file_path,
"type_": TestNodeTypeEnum.class_,
"children": [
{
"id_": "file_two.DiscoveryErrorTwo.test_one",
"name": "test_one",
"path": file_path,
"type_": TestNodeTypeEnum.test,
"lineno": "14",
},
{
"id_": "file_two.DiscoveryErrorTwo.test_two",
"name": "test_two",
"path": file_path,
"type_": TestNodeTypeEnum.test,
"lineno": "17",
},
],
}
],
}
],
}
uuid = "some-uuid"
actual = discover_tests(start_dir, pattern, None, uuid)
assert actual["status"] == "error"
assert actual["uuid"] == uuid
assert is_same_tree(expected, actual.get("tests"))
assert len(actual.get("errors", [])) == 1