Skip to content

Commit 84f334f

Browse files
authored
fix bug that caused dup in param test cases (#21498)
fixes #21491
1 parent 0307e91 commit 84f334f

File tree

6 files changed

+136
-11
lines changed

6 files changed

+136
-11
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize("num", ["a", "b", "c"])
7+
def test_odd_even(num):
8+
assert True
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize("num", range(1, 4))
7+
def test_odd_even(num):
8+
assert True

pythonFiles/tests/pytestadapter/expected_discovery_test_output.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,114 @@
474474
],
475475
"id_": TEST_DATA_PATH_STR,
476476
}
477+
478+
# This is the expected output for the param_same_name tests.
479+
# └── param_same_name
480+
# └── test_param1.py
481+
# └── test_odd_even
482+
# └── [a]
483+
# └── [b]
484+
# └── [c]
485+
# └── test_param2.py
486+
# └── test_odd_even
487+
# └── [1]
488+
# └── [2]
489+
# └── [3]
490+
param1_path = os.fspath(TEST_DATA_PATH / "param_same_name" / "test_param1.py")
491+
param2_path = os.fspath(TEST_DATA_PATH / "param_same_name" / "test_param2.py")
492+
param_same_name_expected_output = {
493+
"name": ".data",
494+
"path": TEST_DATA_PATH_STR,
495+
"type_": "folder",
496+
"children": [
497+
{
498+
"name": "param_same_name",
499+
"path": os.fspath(TEST_DATA_PATH / "param_same_name"),
500+
"type_": "folder",
501+
"id_": os.fspath(TEST_DATA_PATH / "param_same_name"),
502+
"children": [
503+
{
504+
"name": "test_param1.py",
505+
"path": param1_path,
506+
"type_": "file",
507+
"id_": param1_path,
508+
"children": [
509+
{
510+
"name": "test_odd_even",
511+
"path": param1_path,
512+
"type_": "function",
513+
"children": [
514+
{
515+
"name": "[a]",
516+
"path": param1_path,
517+
"lineno": "6",
518+
"type_": "test",
519+
"id_": "param_same_name/test_param1.py::test_odd_even[a]",
520+
"runID": "param_same_name/test_param1.py::test_odd_even[a]",
521+
},
522+
{
523+
"name": "[b]",
524+
"path": param1_path,
525+
"lineno": "6",
526+
"type_": "test",
527+
"id_": "param_same_name/test_param1.py::test_odd_even[b]",
528+
"runID": "param_same_name/test_param1.py::test_odd_even[b]",
529+
},
530+
{
531+
"name": "[c]",
532+
"path": param1_path,
533+
"lineno": "6",
534+
"type_": "test",
535+
"id_": "param_same_name/test_param1.py::test_odd_even[c]",
536+
"runID": "param_same_name/test_param1.py::test_odd_even[c]",
537+
},
538+
],
539+
"id_": "param_same_name/test_param1.py::test_odd_even",
540+
}
541+
],
542+
},
543+
{
544+
"name": "test_param2.py",
545+
"path": param2_path,
546+
"type_": "file",
547+
"id_": param2_path,
548+
"children": [
549+
{
550+
"name": "test_odd_even",
551+
"path": param2_path,
552+
"type_": "function",
553+
"children": [
554+
{
555+
"name": "[1]",
556+
"path": param2_path,
557+
"lineno": "6",
558+
"type_": "test",
559+
"id_": "param_same_name/test_param2.py::test_odd_even[1]",
560+
"runID": "param_same_name/test_param2.py::test_odd_even[1]",
561+
},
562+
{
563+
"name": "[2]",
564+
"path": param2_path,
565+
"lineno": "6",
566+
"type_": "test",
567+
"id_": "param_same_name/test_param2.py::test_odd_even[2]",
568+
"runID": "param_same_name/test_param2.py::test_odd_even[2]",
569+
},
570+
{
571+
"name": "[3]",
572+
"path": param2_path,
573+
"lineno": "6",
574+
"type_": "test",
575+
"id_": "param_same_name/test_param2.py::test_odd_even[3]",
576+
"runID": "param_same_name/test_param2.py::test_odd_even[3]",
577+
},
578+
],
579+
"id_": "param_same_name/test_param2.py::test_odd_even",
580+
}
581+
],
582+
},
583+
],
584+
}
585+
],
586+
"id_": TEST_DATA_PATH_STR,
587+
}

pythonFiles/tests/pytestadapter/test_discovery.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ def test_parameterized_error_collect():
8787
@pytest.mark.parametrize(
8888
"file, expected_const",
8989
[
90+
(
91+
"param_same_name",
92+
expected_discovery_test_output.param_same_name_expected_output,
93+
),
9094
(
9195
"parametrize_tests.py",
9296
expected_discovery_test_output.parametrize_tests_expected_output,

pythonFiles/tests/pytestadapter/test_execution.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,6 @@ def test_bad_id_error_execution():
125125
],
126126
expected_execution_test_output.doctest_pytest_expected_execution_output,
127127
),
128-
(
129-
[
130-
"",
131-
],
132-
expected_execution_test_output.no_test_ids_pytest_execution_expected_output,
133-
),
134128
],
135129
)
136130
def test_pytest_execution(test_ids, expected_const):
@@ -147,7 +141,6 @@ def test_pytest_execution(test_ids, expected_const):
147141
8. parametrize_tests_expected_execution_output: test run on a parametrize test with 3 inputs.
148142
9. single_parametrize_tests_expected_execution_output: test run on single parametrize test.
149143
10. doctest_pytest_expected_execution_output: test run on doctest file.
150-
11. no_test_ids_pytest_execution_expected_output: test run with no inputted test ids.
151144
152145
153146
Keyword arguments:

pythonFiles/vscode_pytest/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,12 @@ def build_test_tree(session: pytest.Session) -> TestNode:
288288
elif hasattr(test_case, "callspec"): # This means it is a parameterized test.
289289
function_name: str = ""
290290
# parameterized test cases cut the repetitive part of the name off.
291-
name_split = test_node["name"].split("[")[1]
292-
test_node["name"] = "[" + name_split
291+
name_split = test_node["name"].split("[")
292+
test_node["name"] = "[" + name_split[1]
293+
parent_path = os.fspath(test_case.path)
293294
try:
294295
function_name = test_case.originalname # type: ignore
295-
function_test_case = function_nodes_dict[function_name]
296+
function_test_case = function_nodes_dict[parent_path]
296297
except AttributeError: # actual error has occurred
297298
ERRORS.append(
298299
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:
304305
function_test_case: TestNode = create_parameterized_function_node(
305306
function_name, test_case.path, test_case.nodeid
306307
)
307-
function_nodes_dict[function_name] = function_test_case
308+
function_nodes_dict[parent_path] = function_test_case
308309
function_test_case["children"].append(test_node)
309310
# Now, add the function node to file node.
310311
try:

0 commit comments

Comments
 (0)