Skip to content

Commit

Permalink
allow -v, -f, -q, --local in unittest args (#22357)
Browse files Browse the repository at this point in the history
fixes #22343
  • Loading branch information
eleanorjboyd authored Oct 27, 2023
1 parent 93bf5cc commit 183a529
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 16 deletions.
30 changes: 27 additions & 3 deletions pythonFiles/tests/unittestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
[
(
["-s", "something", "-p", "other*", "-t", "else"],
("something", "other*", "else"),
("something", "other*", "else", 1, None, None),
),
(
[
Expand All @@ -29,11 +29,35 @@
"--top-level-directory",
"baz",
],
("foo", "bar*", "baz"),
("foo", "bar*", "baz", 1, None, None),
),
(
["--foo", "something"],
(".", "test*.py", None),
(".", "test*.py", None, 1, None, None),
),
(
["--foo", "something", "-v"],
(".", "test*.py", None, 2, None, None),
),
(
["--foo", "something", "-f"],
(".", "test*.py", None, 1, True, None),
),
(
["--foo", "something", "--verbose", "-f"],
(".", "test*.py", None, 2, True, None),
),
(
["--foo", "something", "-q", "--failfast"],
(".", "test*.py", None, 0, True, None),
),
(
["--foo", "something", "--quiet"],
(".", "test*.py", None, 0, None, None),
),
(
["--foo", "something", "--quiet", "--locals"],
(".", "test*.py", None, 0, None, True),
),
],
)
Expand Down
38 changes: 32 additions & 6 deletions pythonFiles/tests/unittestadapter/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_no_ids_run() -> None:
start_dir: str = os.fspath(TEST_DATA_PATH)
testids = []
pattern = "discovery_simple*"
actual = run_tests(start_dir, testids, pattern, None, "fake-uuid")
actual = run_tests(start_dir, testids, pattern, None, "fake-uuid", 1, None)
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
Expand All @@ -41,7 +41,13 @@ def test_single_ids_run() -> None:
"""
id = "discovery_simple.DiscoverySimple.test_one"
actual = run_tests(
os.fspath(TEST_DATA_PATH), [id], "discovery_simple*", None, "fake-uuid"
os.fspath(TEST_DATA_PATH),
[id],
"discovery_simple*",
None,
"fake-uuid",
1,
None,
)
assert actual
assert all(item in actual for item in ("cwd", "status"))
Expand All @@ -65,7 +71,13 @@ def test_subtest_run() -> None:
"""
id = "test_subtest.NumbersTest.test_even"
actual = run_tests(
os.fspath(TEST_DATA_PATH), [id], "test_subtest.py", None, "fake-uuid"
os.fspath(TEST_DATA_PATH),
[id],
"test_subtest.py",
None,
"fake-uuid",
1,
None,
)
subtests_ids = [
"test_subtest.NumbersTest.test_even (i=0)",
Expand Down Expand Up @@ -162,7 +174,7 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
All tests should have the outcome of `success`.
"""
actual = run_tests(cwd, test_ids, pattern, None, "fake-uuid")
actual = run_tests(cwd, test_ids, pattern, None, "fake-uuid", 1, None)
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
Expand All @@ -186,7 +198,13 @@ def test_failed_tests():
"test_fail_simple.RunFailSimple.test_two_fail",
]
actual = run_tests(
os.fspath(TEST_DATA_PATH), test_ids, "test_fail_simple*", None, "fake-uuid"
os.fspath(TEST_DATA_PATH),
test_ids,
"test_fail_simple*",
None,
"fake-uuid",
1,
None,
)
assert actual
assert all(item in actual for item in ("cwd", "status"))
Expand Down Expand Up @@ -214,7 +232,13 @@ def test_unknown_id():
"""
test_ids = ["unknown_id"]
actual = run_tests(
os.fspath(TEST_DATA_PATH), test_ids, "test_fail_simple*", None, "fake-uuid"
os.fspath(TEST_DATA_PATH),
test_ids,
"test_fail_simple*",
None,
"fake-uuid",
1,
None,
)
assert actual
assert all(item in actual for item in ("cwd", "status"))
Expand Down Expand Up @@ -242,6 +266,8 @@ def test_incorrect_path():
"test_fail_simple*",
None,
"fake-uuid",
1,
None,
)
assert actual
assert all(item in actual for item in ("cwd", "status", "error"))
Expand Down
14 changes: 12 additions & 2 deletions pythonFiles/unittestadapter/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class EOTPayloadDict(TypedDict):


def discover_tests(
start_dir: str, pattern: str, top_level_dir: Optional[str], uuid: Optional[str]
start_dir: str,
pattern: str,
top_level_dir: Optional[str],
uuid: Optional[str],
) -> PayloadDict:
"""Returns a dictionary containing details of the discovered tests.
Expand Down Expand Up @@ -119,7 +122,14 @@ def post_response(
argv = sys.argv[1:]
index = argv.index("--udiscovery")

start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])
(
start_dir,
pattern,
top_level_dir,
_verbosity,
_failfast,
_locals,
) = parse_unittest_args(argv[index + 1 :])

testPort = int(os.environ.get("TEST_PORT", DEFAULT_PORT))
testUuid = os.environ.get("TEST_UUID")
Expand Down
35 changes: 31 additions & 4 deletions pythonFiles/unittestadapter/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def run_tests(
pattern: str,
top_level_dir: Optional[str],
uuid: Optional[str],
verbosity: int,
failfast: Optional[bool],
locals: Optional[bool] = None,
) -> PayloadDict:
cwd = os.path.abspath(start_dir)
status = TestExecutionStatus.error
Expand All @@ -190,8 +193,18 @@ def run_tests(
}
suite = loader.discover(start_dir, pattern, top_level_dir) # noqa: F841

# Run tests.
runner = unittest.TextTestRunner(resultclass=UnittestTestResult)
if failfast is None:
failfast = False
if locals is None:
locals = False
if verbosity is None:
verbosity = 1
runner = unittest.TextTestRunner(
resultclass=UnittestTestResult,
tb_locals=locals,
failfast=failfast,
verbosity=verbosity,
)
# lets try to tailer our own suite so we can figure out running only the ones we want
loader = unittest.TestLoader()
tailor: unittest.TestSuite = loader.loadTestsFromNames(test_ids)
Expand Down Expand Up @@ -262,7 +275,14 @@ def post_response(
argv = sys.argv[1:]
index = argv.index("--udiscovery")

start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])
(
start_dir,
pattern,
top_level_dir,
verbosity,
failfast,
locals,
) = parse_unittest_args(argv[index + 1 :])

run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT")
run_test_ids_port_int = (
Expand Down Expand Up @@ -319,7 +339,14 @@ def post_response(
if test_ids_from_buffer:
# Perform test execution.
payload = run_tests(
start_dir, test_ids_from_buffer, pattern, top_level_dir, testUuid
start_dir,
test_ids_from_buffer,
pattern,
top_level_dir,
testUuid,
verbosity,
failfast,
locals,
)
else:
cwd = os.path.abspath(start_dir)
Expand Down
17 changes: 16 additions & 1 deletion pythonFiles/unittestadapter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ def build_test_tree(
return root, error


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

parsed_args, _ = arg_parser.parse_known_args(args)

verbosity: int = 1
if parsed_args.quiet:
verbosity = 0
elif parsed_args.verbose:
verbosity = 2

return (
parsed_args.start_directory,
parsed_args.pattern,
parsed_args.top_level_directory,
verbosity,
parsed_args.failfast,
parsed_args.locals,
)

0 comments on commit 183a529

Please sign in to comment.