Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug that caused dup in param test cases #21498

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
111 changes: 111 additions & 0 deletions pythonFiles/tests/pytestadapter/expected_discovery_test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
4 changes: 4 additions & 0 deletions pythonFiles/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions pythonFiles/tests/pytestadapter/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions pythonFiles/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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:
Expand Down