Skip to content

Commit f77a011

Browse files
authored
fix duplicate class names processed as single node in build test tree pytest (#21601)
fixes #21578
1 parent 1308730 commit f77a011

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

pythonFiles/tests/pytestadapter/.data/unittest_folder/test_add.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ def test_add_positive_numbers(self): # test_marker--test_add_positive_numbers
1919
def test_add_negative_numbers(self): # test_marker--test_add_negative_numbers
2020
result = add(-2, -3)
2121
self.assertEqual(result, -5)
22+
23+
24+
class TestDuplicateFunction(unittest.TestCase):
25+
# This test's id is unittest_folder/test_subtract.py::TestDuplicateFunction::test_dup_a. It has the same class name as
26+
# another test, but it's in a different file, so it should not be confused.
27+
# This test passes.
28+
def test_dup_a(self): # test_marker--test_dup_a
29+
self.assertEqual(1, 1)

pythonFiles/tests/pytestadapter/.data/unittest_folder/test_subtract.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ def test_subtract_negative_numbers( # test_marker--test_subtract_negative_numbe
2424
result = subtract(-2, -3)
2525
# This is intentional to test assertion failures
2626
self.assertEqual(result, 100000)
27+
28+
29+
class TestDuplicateFunction(unittest.TestCase):
30+
# This test's id is unittest_folder/test_subtract.py::TestDuplicateFunction::test_dup_s. It has the same class name as
31+
# another test, but it's in a different file, so it should not be confused.
32+
# This test passes.
33+
def test_dup_s(self): # test_marker--test_dup_s
34+
self.assertEqual(1, 1)

pythonFiles/tests/pytestadapter/expected_discovery_test_output.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@
106106
# │ └── TestAddFunction
107107
# │ ├── test_add_negative_numbers
108108
# │ └── test_add_positive_numbers
109+
# │ └── TestDuplicateFunction
110+
# │ └── test_dup_a
109111
# └── test_subtract.py
110112
# └── TestSubtractFunction
111113
# ├── test_subtract_negative_numbers
112114
# └── test_subtract_positive_numbers
115+
# │ └── TestDuplicateFunction
116+
# │ └── test_dup_s
113117
unittest_folder_path = os.fspath(TEST_DATA_PATH / "unittest_folder")
114118
test_add_path = os.fspath(TEST_DATA_PATH / "unittest_folder" / "test_add.py")
115119
test_subtract_path = os.fspath(TEST_DATA_PATH / "unittest_folder" / "test_subtract.py")
@@ -159,7 +163,26 @@
159163
},
160164
],
161165
"id_": "unittest_folder/test_add.py::TestAddFunction",
162-
}
166+
},
167+
{
168+
"name": "TestDuplicateFunction",
169+
"path": test_add_path,
170+
"type_": "class",
171+
"children": [
172+
{
173+
"name": "test_dup_a",
174+
"path": test_add_path,
175+
"lineno": find_test_line_number(
176+
"test_dup_a",
177+
test_add_path,
178+
),
179+
"type_": "test",
180+
"id_": "unittest_folder/test_add.py::TestDuplicateFunction::test_dup_a",
181+
"runID": "unittest_folder/test_add.py::TestDuplicateFunction::test_dup_a",
182+
},
183+
],
184+
"id_": "unittest_folder/test_add.py::TestDuplicateFunction",
185+
},
163186
],
164187
},
165188
{
@@ -197,7 +220,26 @@
197220
},
198221
],
199222
"id_": "unittest_folder/test_subtract.py::TestSubtractFunction",
200-
}
223+
},
224+
{
225+
"name": "TestDuplicateFunction",
226+
"path": test_subtract_path,
227+
"type_": "class",
228+
"children": [
229+
{
230+
"name": "test_dup_s",
231+
"path": test_subtract_path,
232+
"lineno": find_test_line_number(
233+
"test_dup_s",
234+
test_subtract_path,
235+
),
236+
"type_": "test",
237+
"id_": "unittest_folder/test_subtract.py::TestDuplicateFunction::test_dup_s",
238+
"runID": "unittest_folder/test_subtract.py::TestDuplicateFunction::test_dup_s",
239+
},
240+
],
241+
"id_": "unittest_folder/test_subtract.py::TestDuplicateFunction",
242+
},
201243
],
202244
},
203245
],
@@ -206,6 +248,7 @@
206248
"id_": TEST_DATA_PATH_STR,
207249
}
208250

251+
209252
# This is the expected output for the dual_level_nested_folder tests
210253
# └── dual_level_nested_folder
211254
# └── test_top_folder.py

pythonFiles/vscode_pytest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ def build_test_tree(session: pytest.Session) -> TestNode:
266266
test_node = create_test_node(test_case)
267267
if isinstance(test_case.parent, pytest.Class):
268268
try:
269-
test_class_node = class_nodes_dict[test_case.parent.name]
269+
test_class_node = class_nodes_dict[test_case.parent.nodeid]
270270
except KeyError:
271271
test_class_node = create_class_node(test_case.parent)
272-
class_nodes_dict[test_case.parent.name] = test_class_node
272+
class_nodes_dict[test_case.parent.nodeid] = test_class_node
273273
test_class_node["children"].append(test_node)
274274
if test_case.parent.parent:
275275
parent_module = test_case.parent.parent

0 commit comments

Comments
 (0)