Skip to content

Commit 183a529

Browse files
authored
allow -v, -f, -q, --local in unittest args (microsoft#22357)
fixes microsoft#22343
1 parent 93bf5cc commit 183a529

File tree

5 files changed

+118
-16
lines changed

5 files changed

+118
-16
lines changed

pythonFiles/tests/unittestadapter/test_discovery.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
[
1919
(
2020
["-s", "something", "-p", "other*", "-t", "else"],
21-
("something", "other*", "else"),
21+
("something", "other*", "else", 1, None, None),
2222
),
2323
(
2424
[
@@ -29,11 +29,35 @@
2929
"--top-level-directory",
3030
"baz",
3131
],
32-
("foo", "bar*", "baz"),
32+
("foo", "bar*", "baz", 1, None, None),
3333
),
3434
(
3535
["--foo", "something"],
36-
(".", "test*.py", None),
36+
(".", "test*.py", None, 1, None, None),
37+
),
38+
(
39+
["--foo", "something", "-v"],
40+
(".", "test*.py", None, 2, None, None),
41+
),
42+
(
43+
["--foo", "something", "-f"],
44+
(".", "test*.py", None, 1, True, None),
45+
),
46+
(
47+
["--foo", "something", "--verbose", "-f"],
48+
(".", "test*.py", None, 2, True, None),
49+
),
50+
(
51+
["--foo", "something", "-q", "--failfast"],
52+
(".", "test*.py", None, 0, True, None),
53+
),
54+
(
55+
["--foo", "something", "--quiet"],
56+
(".", "test*.py", None, 0, None, None),
57+
),
58+
(
59+
["--foo", "something", "--quiet", "--locals"],
60+
(".", "test*.py", None, 0, None, True),
3761
),
3862
],
3963
)

pythonFiles/tests/unittestadapter/test_execution.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_no_ids_run() -> None:
2222
start_dir: str = os.fspath(TEST_DATA_PATH)
2323
testids = []
2424
pattern = "discovery_simple*"
25-
actual = run_tests(start_dir, testids, pattern, None, "fake-uuid")
25+
actual = run_tests(start_dir, testids, pattern, None, "fake-uuid", 1, None)
2626
assert actual
2727
assert all(item in actual for item in ("cwd", "status"))
2828
assert actual["status"] == "success"
@@ -41,7 +41,13 @@ def test_single_ids_run() -> None:
4141
"""
4242
id = "discovery_simple.DiscoverySimple.test_one"
4343
actual = run_tests(
44-
os.fspath(TEST_DATA_PATH), [id], "discovery_simple*", None, "fake-uuid"
44+
os.fspath(TEST_DATA_PATH),
45+
[id],
46+
"discovery_simple*",
47+
None,
48+
"fake-uuid",
49+
1,
50+
None,
4551
)
4652
assert actual
4753
assert all(item in actual for item in ("cwd", "status"))
@@ -65,7 +71,13 @@ def test_subtest_run() -> None:
6571
"""
6672
id = "test_subtest.NumbersTest.test_even"
6773
actual = run_tests(
68-
os.fspath(TEST_DATA_PATH), [id], "test_subtest.py", None, "fake-uuid"
74+
os.fspath(TEST_DATA_PATH),
75+
[id],
76+
"test_subtest.py",
77+
None,
78+
"fake-uuid",
79+
1,
80+
None,
6981
)
7082
subtests_ids = [
7183
"test_subtest.NumbersTest.test_even (i=0)",
@@ -162,7 +174,7 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
162174
163175
All tests should have the outcome of `success`.
164176
"""
165-
actual = run_tests(cwd, test_ids, pattern, None, "fake-uuid")
177+
actual = run_tests(cwd, test_ids, pattern, None, "fake-uuid", 1, None)
166178
assert actual
167179
assert all(item in actual for item in ("cwd", "status"))
168180
assert actual["status"] == "success"
@@ -186,7 +198,13 @@ def test_failed_tests():
186198
"test_fail_simple.RunFailSimple.test_two_fail",
187199
]
188200
actual = run_tests(
189-
os.fspath(TEST_DATA_PATH), test_ids, "test_fail_simple*", None, "fake-uuid"
201+
os.fspath(TEST_DATA_PATH),
202+
test_ids,
203+
"test_fail_simple*",
204+
None,
205+
"fake-uuid",
206+
1,
207+
None,
190208
)
191209
assert actual
192210
assert all(item in actual for item in ("cwd", "status"))
@@ -214,7 +232,13 @@ def test_unknown_id():
214232
"""
215233
test_ids = ["unknown_id"]
216234
actual = run_tests(
217-
os.fspath(TEST_DATA_PATH), test_ids, "test_fail_simple*", None, "fake-uuid"
235+
os.fspath(TEST_DATA_PATH),
236+
test_ids,
237+
"test_fail_simple*",
238+
None,
239+
"fake-uuid",
240+
1,
241+
None,
218242
)
219243
assert actual
220244
assert all(item in actual for item in ("cwd", "status"))
@@ -242,6 +266,8 @@ def test_incorrect_path():
242266
"test_fail_simple*",
243267
None,
244268
"fake-uuid",
269+
1,
270+
None,
245271
)
246272
assert actual
247273
assert all(item in actual for item in ("cwd", "status", "error"))

pythonFiles/unittestadapter/discovery.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ class EOTPayloadDict(TypedDict):
3737

3838

3939
def discover_tests(
40-
start_dir: str, pattern: str, top_level_dir: Optional[str], uuid: Optional[str]
40+
start_dir: str,
41+
pattern: str,
42+
top_level_dir: Optional[str],
43+
uuid: Optional[str],
4144
) -> PayloadDict:
4245
"""Returns a dictionary containing details of the discovered tests.
4346
@@ -119,7 +122,14 @@ def post_response(
119122
argv = sys.argv[1:]
120123
index = argv.index("--udiscovery")
121124

122-
start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])
125+
(
126+
start_dir,
127+
pattern,
128+
top_level_dir,
129+
_verbosity,
130+
_failfast,
131+
_locals,
132+
) = parse_unittest_args(argv[index + 1 :])
123133

124134
testPort = int(os.environ.get("TEST_PORT", DEFAULT_PORT))
125135
testUuid = os.environ.get("TEST_UUID")

pythonFiles/unittestadapter/execution.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ def run_tests(
167167
pattern: str,
168168
top_level_dir: Optional[str],
169169
uuid: Optional[str],
170+
verbosity: int,
171+
failfast: Optional[bool],
172+
locals: Optional[bool] = None,
170173
) -> PayloadDict:
171174
cwd = os.path.abspath(start_dir)
172175
status = TestExecutionStatus.error
@@ -190,8 +193,18 @@ def run_tests(
190193
}
191194
suite = loader.discover(start_dir, pattern, top_level_dir) # noqa: F841
192195

193-
# Run tests.
194-
runner = unittest.TextTestRunner(resultclass=UnittestTestResult)
196+
if failfast is None:
197+
failfast = False
198+
if locals is None:
199+
locals = False
200+
if verbosity is None:
201+
verbosity = 1
202+
runner = unittest.TextTestRunner(
203+
resultclass=UnittestTestResult,
204+
tb_locals=locals,
205+
failfast=failfast,
206+
verbosity=verbosity,
207+
)
195208
# lets try to tailer our own suite so we can figure out running only the ones we want
196209
loader = unittest.TestLoader()
197210
tailor: unittest.TestSuite = loader.loadTestsFromNames(test_ids)
@@ -262,7 +275,14 @@ def post_response(
262275
argv = sys.argv[1:]
263276
index = argv.index("--udiscovery")
264277

265-
start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])
278+
(
279+
start_dir,
280+
pattern,
281+
top_level_dir,
282+
verbosity,
283+
failfast,
284+
locals,
285+
) = parse_unittest_args(argv[index + 1 :])
266286

267287
run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT")
268288
run_test_ids_port_int = (
@@ -319,7 +339,14 @@ def post_response(
319339
if test_ids_from_buffer:
320340
# Perform test execution.
321341
payload = run_tests(
322-
start_dir, test_ids_from_buffer, pattern, top_level_dir, testUuid
342+
start_dir,
343+
test_ids_from_buffer,
344+
pattern,
345+
top_level_dir,
346+
testUuid,
347+
verbosity,
348+
failfast,
349+
locals,
323350
)
324351
else:
325352
cwd = os.path.abspath(start_dir)

pythonFiles/unittestadapter/utils.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ def build_test_tree(
217217
return root, error
218218

219219

220-
def parse_unittest_args(args: List[str]) -> Tuple[str, str, Union[str, None]]:
220+
def parse_unittest_args(
221+
args: List[str],
222+
) -> Tuple[str, str, Union[str, None], int, Union[bool, None], Union[bool, None]]:
221223
"""Parse command-line arguments that should be forwarded to unittest to perform discovery.
222224
223225
Valid unittest arguments are: -v, -s, -p, -t and their long-form counterparts,
@@ -234,11 +236,24 @@ def parse_unittest_args(args: List[str]) -> Tuple[str, str, Union[str, None]]:
234236
arg_parser.add_argument("--start-directory", "-s", default=".")
235237
arg_parser.add_argument("--pattern", "-p", default="test*.py")
236238
arg_parser.add_argument("--top-level-directory", "-t", default=None)
239+
arg_parser.add_argument("--failfast", "-f", action="store_true", default=None)
240+
arg_parser.add_argument("--verbose", "-v", action="store_true", default=None)
241+
arg_parser.add_argument("-q", "--quiet", action="store_true", default=None)
242+
arg_parser.add_argument("--locals", action="store_true", default=None)
237243

238244
parsed_args, _ = arg_parser.parse_known_args(args)
239245

246+
verbosity: int = 1
247+
if parsed_args.quiet:
248+
verbosity = 0
249+
elif parsed_args.verbose:
250+
verbosity = 2
251+
240252
return (
241253
parsed_args.start_directory,
242254
parsed_args.pattern,
243255
parsed_args.top_level_directory,
256+
verbosity,
257+
parsed_args.failfast,
258+
parsed_args.locals,
244259
)

0 commit comments

Comments
 (0)