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..33440a6d42fa 100644 --- a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py +++ b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py @@ -474,3 +474,114 @@ ], "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] +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, + "type_": "folder", + "children": [ + { + "name": "param_same_name", + "path": os.fspath(TEST_DATA_PATH / "param_same_name"), + "type_": "folder", + "id_": os.fspath(TEST_DATA_PATH / "param_same_name"), + "children": [ + { + "name": "test_param1.py", + "path": param1_path, + "type_": "file", + "id_": param1_path, + "children": [ + { + "name": "test_odd_even", + "path": param1_path, + "type_": "function", + "children": [ + { + "name": "[a]", + "path": param1_path, + "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": param1_path, + "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": param1_path, + "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": param2_path, + "type_": "file", + "id_": param2_path, + "children": [ + { + "name": "test_odd_even", + "path": param2_path, + "type_": "function", + "children": [ + { + "name": "[1]", + "path": param2_path, + "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": param2_path, + "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": param2_path, + "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: