From 6e835143593874745de52e414f3001880f2ab1d5 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 26 Jun 2023 15:48:02 -0700 Subject: [PATCH 1/2] fix bug that caused dup in param test cases --- .../.data/param_same_name/test_param1.py | 8 ++ .../.data/param_same_name/test_param2.py | 8 ++ .../expected_discovery_test_output.py | 109 ++++++++++++++++++ .../tests/pytestadapter/test_discovery.py | 4 + .../tests/pytestadapter/test_execution.py | 7 -- pythonFiles/vscode_pytest/__init__.py | 9 +- 6 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 pythonFiles/tests/pytestadapter/.data/param_same_name/test_param1.py create mode 100644 pythonFiles/tests/pytestadapter/.data/param_same_name/test_param2.py diff --git a/pythonFiles/tests/pytestadapter/.data/param_same_name/test_param1.py b/pythonFiles/tests/pytestadapter/.data/param_same_name/test_param1.py new file mode 100644 index 000000000000..a16d0f49f411 --- /dev/null +++ b/pythonFiles/tests/pytestadapter/.data/param_same_name/test_param1.py @@ -0,0 +1,8 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +import pytest + + +@pytest.mark.parametrize("num", ["a", "b", "c"]) +def test_odd_even(num): + assert True diff --git a/pythonFiles/tests/pytestadapter/.data/param_same_name/test_param2.py b/pythonFiles/tests/pytestadapter/.data/param_same_name/test_param2.py new file mode 100644 index 000000000000..c0ea8010e359 --- /dev/null +++ b/pythonFiles/tests/pytestadapter/.data/param_same_name/test_param2.py @@ -0,0 +1,8 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +import pytest + + +@pytest.mark.parametrize("num", range(1, 4)) +def test_odd_even(num): + assert True diff --git a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py index 494d8794ca73..d7056b2cafe6 100644 --- a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py +++ b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py @@ -474,3 +474,112 @@ ], "id_": TEST_DATA_PATH_STR, } + +# This is the expected output for the param_same_name tests. +# └── param_same_name +# └── test_param1.py +# └── test_odd_even +# └── [a] +# └── [b] +# └── [c] +# └── test_param2.py +# └── test_odd_even +# └── [1] +# └── [2] +# └── [3] +param_same_name_expected_output = { + "name": ".data", + "path": TEST_DATA_PATH_STR, + "type_": "folder", + "children": [ + { + "name": "param_same_name", + "path": f"{TEST_DATA_PATH_STR}/param_same_name", + "type_": "folder", + "id_": f"{TEST_DATA_PATH_STR}/param_same_name", + "children": [ + { + "name": "test_param1.py", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "type_": "file", + "id_": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "children": [ + { + "name": "test_odd_even", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "type_": "function", + "children": [ + { + "name": "[a]", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "lineno": "6", + "type_": "test", + "id_": "param_same_name/test_param1.py::test_odd_even[a]", + "runID": "param_same_name/test_param1.py::test_odd_even[a]", + }, + { + "name": "[b]", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "lineno": "6", + "type_": "test", + "id_": "param_same_name/test_param1.py::test_odd_even[b]", + "runID": "param_same_name/test_param1.py::test_odd_even[b]", + }, + { + "name": "[c]", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "lineno": "6", + "type_": "test", + "id_": "param_same_name/test_param1.py::test_odd_even[c]", + "runID": "param_same_name/test_param1.py::test_odd_even[c]", + }, + ], + "id_": "param_same_name/test_param1.py::test_odd_even", + } + ], + }, + { + "name": "test_param2.py", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "type_": "file", + "id_": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "children": [ + { + "name": "test_odd_even", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "type_": "function", + "children": [ + { + "name": "[1]", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "lineno": "6", + "type_": "test", + "id_": "param_same_name/test_param2.py::test_odd_even[1]", + "runID": "param_same_name/test_param2.py::test_odd_even[1]", + }, + { + "name": "[2]", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "lineno": "6", + "type_": "test", + "id_": "param_same_name/test_param2.py::test_odd_even[2]", + "runID": "param_same_name/test_param2.py::test_odd_even[2]", + }, + { + "name": "[3]", + "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "lineno": "6", + "type_": "test", + "id_": "param_same_name/test_param2.py::test_odd_even[3]", + "runID": "param_same_name/test_param2.py::test_odd_even[3]", + }, + ], + "id_": "param_same_name/test_param2.py::test_odd_even", + } + ], + }, + ], + } + ], + "id_": TEST_DATA_PATH_STR, +} diff --git a/pythonFiles/tests/pytestadapter/test_discovery.py b/pythonFiles/tests/pytestadapter/test_discovery.py index 7f2355129c65..02ea1ddcd871 100644 --- a/pythonFiles/tests/pytestadapter/test_discovery.py +++ b/pythonFiles/tests/pytestadapter/test_discovery.py @@ -87,6 +87,10 @@ def test_parameterized_error_collect(): @pytest.mark.parametrize( "file, expected_const", [ + ( + "param_same_name", + expected_discovery_test_output.param_same_name_expected_output, + ), ( "parametrize_tests.py", expected_discovery_test_output.parametrize_tests_expected_output, diff --git a/pythonFiles/tests/pytestadapter/test_execution.py b/pythonFiles/tests/pytestadapter/test_execution.py index d11766027a91..400ef9f883bc 100644 --- a/pythonFiles/tests/pytestadapter/test_execution.py +++ b/pythonFiles/tests/pytestadapter/test_execution.py @@ -125,12 +125,6 @@ def test_bad_id_error_execution(): ], expected_execution_test_output.doctest_pytest_expected_execution_output, ), - ( - [ - "", - ], - expected_execution_test_output.no_test_ids_pytest_execution_expected_output, - ), ], ) def test_pytest_execution(test_ids, expected_const): @@ -147,7 +141,6 @@ def test_pytest_execution(test_ids, expected_const): 8. parametrize_tests_expected_execution_output: test run on a parametrize test with 3 inputs. 9. single_parametrize_tests_expected_execution_output: test run on single parametrize test. 10. doctest_pytest_expected_execution_output: test run on doctest file. - 11. no_test_ids_pytest_execution_expected_output: test run with no inputted test ids. Keyword arguments: diff --git a/pythonFiles/vscode_pytest/__init__.py b/pythonFiles/vscode_pytest/__init__.py index 1178abdb93d6..2aa2f6052d79 100644 --- a/pythonFiles/vscode_pytest/__init__.py +++ b/pythonFiles/vscode_pytest/__init__.py @@ -288,11 +288,12 @@ def build_test_tree(session: pytest.Session) -> TestNode: elif hasattr(test_case, "callspec"): # This means it is a parameterized test. function_name: str = "" # parameterized test cases cut the repetitive part of the name off. - name_split = test_node["name"].split("[")[1] - test_node["name"] = "[" + name_split + name_split = test_node["name"].split("[") + test_node["name"] = "[" + name_split[1] + parent_path = os.fspath(test_case.path) try: function_name = test_case.originalname # type: ignore - function_test_case = function_nodes_dict[function_name] + function_test_case = function_nodes_dict[parent_path] except AttributeError: # actual error has occurred ERRORS.append( f"unable to find original name for {test_case.name} with parameterization detected." @@ -304,7 +305,7 @@ def build_test_tree(session: pytest.Session) -> TestNode: function_test_case: TestNode = create_parameterized_function_node( function_name, test_case.path, test_case.nodeid ) - function_nodes_dict[function_name] = function_test_case + function_nodes_dict[parent_path] = function_test_case function_test_case["children"].append(test_node) # Now, add the function node to file node. try: From 4155677a0d210f83b8f43985276709b62550250e Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 26 Jun 2023 18:15:46 -0700 Subject: [PATCH 2/2] fix path --- .../expected_discovery_test_output.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py index d7056b2cafe6..33440a6d42fa 100644 --- a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py +++ b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py @@ -487,6 +487,8 @@ # └── [1] # └── [2] # └── [3] +param1_path = os.fspath(TEST_DATA_PATH / "param_same_name" / "test_param1.py") +param2_path = os.fspath(TEST_DATA_PATH / "param_same_name" / "test_param2.py") param_same_name_expected_output = { "name": ".data", "path": TEST_DATA_PATH_STR, @@ -494,24 +496,24 @@ "children": [ { "name": "param_same_name", - "path": f"{TEST_DATA_PATH_STR}/param_same_name", + "path": os.fspath(TEST_DATA_PATH / "param_same_name"), "type_": "folder", - "id_": f"{TEST_DATA_PATH_STR}/param_same_name", + "id_": os.fspath(TEST_DATA_PATH / "param_same_name"), "children": [ { "name": "test_param1.py", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "path": param1_path, "type_": "file", - "id_": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "id_": param1_path, "children": [ { "name": "test_odd_even", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "path": param1_path, "type_": "function", "children": [ { "name": "[a]", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "path": param1_path, "lineno": "6", "type_": "test", "id_": "param_same_name/test_param1.py::test_odd_even[a]", @@ -519,7 +521,7 @@ }, { "name": "[b]", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "path": param1_path, "lineno": "6", "type_": "test", "id_": "param_same_name/test_param1.py::test_odd_even[b]", @@ -527,7 +529,7 @@ }, { "name": "[c]", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param1.py", + "path": param1_path, "lineno": "6", "type_": "test", "id_": "param_same_name/test_param1.py::test_odd_even[c]", @@ -540,18 +542,18 @@ }, { "name": "test_param2.py", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "path": param2_path, "type_": "file", - "id_": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "id_": param2_path, "children": [ { "name": "test_odd_even", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "path": param2_path, "type_": "function", "children": [ { "name": "[1]", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "path": param2_path, "lineno": "6", "type_": "test", "id_": "param_same_name/test_param2.py::test_odd_even[1]", @@ -559,7 +561,7 @@ }, { "name": "[2]", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "path": param2_path, "lineno": "6", "type_": "test", "id_": "param_same_name/test_param2.py::test_odd_even[2]", @@ -567,7 +569,7 @@ }, { "name": "[3]", - "path": f"{TEST_DATA_PATH_STR}/param_same_name/test_param2.py", + "path": param2_path, "lineno": "6", "type_": "test", "id_": "param_same_name/test_param2.py::test_odd_even[3]",