Skip to content

Commit 983f05a

Browse files
Add testing for unittest execution python logic (microsoft#21022)
these tests cover: - parsing execution args for unittest - test run with no test_ids attached - test run with single test_id and test is a success. --------- Co-authored-by: Karthik Nadig <kanadig@microsoft.com>
1 parent c64bb0e commit 983f05a

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import os
5+
import pathlib
6+
from typing import List
7+
8+
import pytest
9+
from unittestadapter.execution import parse_execution_cli_args, run_tests
10+
11+
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
12+
13+
14+
@pytest.mark.parametrize(
15+
"args, expected",
16+
[
17+
(
18+
[
19+
"--port",
20+
"111",
21+
"--uuid",
22+
"fake-uuid",
23+
"--testids",
24+
"test_file.test_class.test_method",
25+
],
26+
(111, "fake-uuid", ["test_file.test_class.test_method"]),
27+
),
28+
(
29+
["--port", "111", "--uuid", "fake-uuid", "--testids", ""],
30+
(111, "fake-uuid", [""]),
31+
),
32+
(
33+
[
34+
"--port",
35+
"111",
36+
"--uuid",
37+
"fake-uuid",
38+
"--testids",
39+
"test_file.test_class.test_method",
40+
"-v",
41+
"-s",
42+
],
43+
(111, "fake-uuid", ["test_file.test_class.test_method"]),
44+
),
45+
],
46+
)
47+
def test_parse_execution_cli_args(args: List[str], expected: List[str]) -> None:
48+
"""The parse_execution_cli_args function should return values for the port, uuid, and testids arguments
49+
when passed as command-line options, and ignore unrecognized arguments.
50+
"""
51+
actual = parse_execution_cli_args(args)
52+
assert actual == expected
53+
54+
55+
def test_no_ids_run() -> None:
56+
"""This test runs on an empty array of test_ids, therefore it should return
57+
an empty dict for the result.
58+
"""
59+
start_dir: str = os.fspath(TEST_DATA_PATH)
60+
testids = []
61+
pattern = "discovery_simple*"
62+
actual = run_tests(start_dir, testids, pattern, None, "fake-uuid")
63+
assert actual
64+
assert all(item in actual for item in ("cwd", "status"))
65+
assert actual["status"] == "success"
66+
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
67+
if "result" in actual:
68+
assert len(actual["result"]) == 0
69+
else:
70+
raise AssertionError("actual['result'] is None")
71+
72+
73+
def test_single_ids_run() -> None:
74+
"""This test runs on a single test_id, therefore it should return
75+
a dict with a single key-value pair for the result.
76+
77+
This single test passes so the outcome should be 'success'.
78+
"""
79+
id = "discovery_simple.DiscoverySimple.test_one"
80+
actual = run_tests(
81+
os.fspath(TEST_DATA_PATH), [id], "discovery_simple*", None, "fake-uuid"
82+
)
83+
assert actual
84+
assert all(item in actual for item in ("cwd", "status"))
85+
assert actual["status"] == "success"
86+
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
87+
assert "result" in actual
88+
result = actual["result"]
89+
assert len(result) == 1
90+
assert id in result
91+
id_result = result[id]
92+
assert id_result is not None
93+
assert "outcome" in id_result
94+
assert id_result["outcome"] == "success"

pythonFiles/unittestadapter/execution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ def run_tests(
206206

207207
if error is not None:
208208
payload["error"] = error
209+
else:
210+
status = TestExecutionStatus.success
209211

210212
payload["status"] = status
211213

212-
# print(f"payload: \n{json.dumps(payload, indent=4)}")
213-
214214
return payload
215215

216216

0 commit comments

Comments
 (0)