diff --git a/pythonFiles/tests/unittestadapter/test_discovery.py b/pythonFiles/tests/unittestadapter/test_discovery.py index 30ccb7ef4079..28dc51f55dcd 100644 --- a/pythonFiles/tests/unittestadapter/test_discovery.py +++ b/pythonFiles/tests/unittestadapter/test_discovery.py @@ -132,7 +132,7 @@ def test_simple_discovery() -> None: assert actual["status"] == "success" assert is_same_tree(actual.get("tests"), expected) - assert "errors" not in actual + assert "error" not in actual def test_empty_discovery() -> None: @@ -146,8 +146,8 @@ def test_empty_discovery() -> None: actual = discover_tests(start_dir, pattern, None, uuid) assert actual["status"] == "success" - assert "tests" not in actual - assert "errors" not in actual + assert "tests" in actual + assert "error" not in actual def test_error_discovery() -> None: @@ -213,4 +213,4 @@ def test_error_discovery() -> None: assert actual["status"] == "error" assert is_same_tree(expected, actual.get("tests")) - assert len(actual.get("errors", [])) == 1 + assert len(actual.get("error", [])) == 1 diff --git a/pythonFiles/tests/unittestadapter/test_execution.py b/pythonFiles/tests/unittestadapter/test_execution.py index c7a5cb0eef61..a822636bf479 100644 --- a/pythonFiles/tests/unittestadapter/test_execution.py +++ b/pythonFiles/tests/unittestadapter/test_execution.py @@ -65,7 +65,7 @@ def test_no_ids_run() -> None: assert all(item in actual for item in ("cwd", "status")) assert actual["status"] == "success" assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - if "result" in actual: + if actual["result"] is not None: assert len(actual["result"]) == 0 else: raise AssertionError("actual['result'] is None") @@ -85,7 +85,7 @@ def test_single_ids_run() -> None: assert all(item in actual for item in ("cwd", "status")) assert actual["status"] == "success" assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert "result" in actual + assert actual["result"] is not None result = actual["result"] assert len(result) == 1 assert id in result @@ -117,7 +117,7 @@ def test_subtest_run() -> None: assert all(item in actual for item in ("cwd", "status")) assert actual["status"] == "success" assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert "result" in actual + assert actual["result"] is not None result = actual["result"] assert len(result) == 6 for id in subtests_ids: @@ -205,7 +205,7 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None: assert all(item in actual for item in ("cwd", "status")) assert actual["status"] == "success" assert actual["cwd"] == cwd - assert "result" in actual + assert actual["result"] is not None result = actual["result"] assert len(result) == len(test_ids) for test_id in test_ids: @@ -230,7 +230,7 @@ def test_failed_tests(): assert all(item in actual for item in ("cwd", "status")) assert actual["status"] == "success" assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert "result" in actual + assert actual["result"] is not None result = actual["result"] assert len(result) == len(test_ids) for test_id in test_ids: @@ -255,7 +255,7 @@ def test_unknown_id(): assert all(item in actual for item in ("cwd", "status")) assert actual["status"] == "success" assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert "result" in actual + assert actual["result"] is not None result = actual["result"] assert len(result) == len(test_ids) assert "unittest.loader._FailedTest.unknown_id" in result diff --git a/pythonFiles/unittestadapter/discovery.py b/pythonFiles/unittestadapter/discovery.py index 02490024b217..ffcd2671addb 100644 --- a/pythonFiles/unittestadapter/discovery.py +++ b/pythonFiles/unittestadapter/discovery.py @@ -53,8 +53,8 @@ def parse_discovery_cli_args(args: List[str]) -> Tuple[int, Union[str, None]]: class PayloadDict(TypedDict): cwd: str status: Literal["success", "error"] - tests: NotRequired[TestNode] - errors: NotRequired[List[str]] + tests: Optional[TestNode] + error: NotRequired[List[str]] def discover_tests( @@ -68,7 +68,7 @@ def discover_tests( - uuid: UUID sent by the caller of the Python script, that needs to be sent back as an integrity check; - status: Test discovery status, can be "success" or "error"; - tests: Discoverered tests if any, not present otherwise. Note that the status can be "error" but the payload can still contain tests; - - errors: Discovery errors if any, not present otherwise. + - error: Discovery error if any, not present otherwise. Payload format for a successful discovery: { @@ -86,30 +86,31 @@ def discover_tests( Payload format when there are errors: { "cwd": - "errors": [list of errors] + "": [list of errors] "status": "error", } """ cwd = os.path.abspath(start_dir) - payload: PayloadDict = {"cwd": cwd, "status": "success"} + payload: PayloadDict = {"cwd": cwd, "status": "success", "tests": None} tests = None - errors: List[str] = [] + error: List[str] = [] try: loader = unittest.TestLoader() suite = loader.discover(start_dir, pattern, top_level_dir) - tests, errors = build_test_tree(suite, cwd) # test tree built succesfully here. + tests, error = build_test_tree(suite, cwd) # test tree built succesfully here. except Exception: - errors.append(traceback.format_exc()) + error.append(traceback.format_exc()) - if tests is not None: - payload["tests"] = tests + # Still include the tests in the payload even if there are errors so that the TS + # side can determine if it is from run or discovery. + payload["tests"] = tests if tests is not None else None - if len(errors): + if len(error): payload["status"] = "error" - payload["errors"] = errors + payload["error"] = error return payload diff --git a/pythonFiles/unittestadapter/execution.py b/pythonFiles/unittestadapter/execution.py index d9d5f41d624b..aa50cbc5d09f 100644 --- a/pythonFiles/unittestadapter/execution.py +++ b/pythonFiles/unittestadapter/execution.py @@ -165,7 +165,7 @@ class TestExecutionStatus(str, enum.Enum): class PayloadDict(TypedDict): cwd: str status: TestExecutionStatus - result: NotRequired[TestResultTypeAlias] + result: Optional[TestResultTypeAlias] not_found: NotRequired[List[str]] error: NotRequired[str] @@ -185,7 +185,7 @@ def run_tests( cwd = os.path.abspath(start_dir) status = TestExecutionStatus.error error = None - payload: PayloadDict = {"cwd": cwd, "status": status} + payload: PayloadDict = {"cwd": cwd, "status": status, "result": None} try: # If it's a file, split path and file name. @@ -312,4 +312,5 @@ def send_run_data(raw_data, port, uuid): "cwd": cwd, "status": status, "error": "No test ids received from buffer", + "result": None, } diff --git a/pythonFiles/unittestadapter/utils.py b/pythonFiles/unittestadapter/utils.py index 9c8b896a8d6e..a461baf7d870 100644 --- a/pythonFiles/unittestadapter/utils.py +++ b/pythonFiles/unittestadapter/utils.py @@ -151,14 +151,14 @@ def build_test_tree( "id_": } """ - errors = [] + error = [] directory_path = pathlib.PurePath(test_directory) root = build_test_node(test_directory, directory_path.name, TestNodeTypeEnum.folder) for test_case in get_test_case(suite): test_id = test_case.id() if test_id.startswith("unittest.loader._FailedTest"): - errors.append(str(test_case._exception)) # type: ignore + error.append(str(test_case._exception)) # type: ignore else: # Get the static test path components: filename, class name and function name. components = test_id.split(".") @@ -206,7 +206,7 @@ def build_test_tree( if not root["children"]: root = None - return root, errors + return root, error def parse_unittest_args(args: List[str]) -> Tuple[str, str, Union[str, None]]: