Skip to content

Commit 520f396

Browse files
authored
switch to use file path as key in building test tree (#24697)
precursor for #23933
1 parent ef6ca9f commit 520f396

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

python_files/vscode_pytest/__init__.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
495495
"""
496496
session_node = create_session_node(session)
497497
session_children_dict: dict[str, TestNode] = {}
498-
file_nodes_dict: dict[Any, TestNode] = {}
498+
file_nodes_dict: dict[str, TestNode] = {}
499499
class_nodes_dict: dict[str, TestNode] = {}
500500
function_nodes_dict: dict[str, TestNode] = {}
501501

@@ -544,11 +544,13 @@ def build_test_tree(session: pytest.Session) -> TestNode:
544544
function_test_node["children"].append(test_node)
545545
# Check if the parent node of the function is file, if so create/add to this file node.
546546
if isinstance(test_case.parent, pytest.File):
547+
# calculate the parent path of the test case
548+
parent_path = get_node_path(test_case.parent)
547549
try:
548-
parent_test_case = file_nodes_dict[test_case.parent]
550+
parent_test_case = file_nodes_dict[os.fspath(parent_path)]
549551
except KeyError:
550-
parent_test_case = create_file_node(test_case.parent)
551-
file_nodes_dict[test_case.parent] = parent_test_case
552+
parent_test_case = create_file_node(parent_path)
553+
file_nodes_dict[os.fspath(parent_path)] = parent_test_case
552554
if function_test_node not in parent_test_case["children"]:
553555
parent_test_case["children"].append(function_test_node)
554556
# If the parent is not a file, it is a class, add the function node as the test node to handle subsequent nesting.
@@ -580,22 +582,24 @@ def build_test_tree(session: pytest.Session) -> TestNode:
580582
else:
581583
ERRORS.append(f"Test class {case_iter} has no parent")
582584
break
585+
parent_path = get_node_path(parent_module)
583586
# Create a file node that has the last class as a child.
584587
try:
585-
test_file_node: TestNode = file_nodes_dict[parent_module]
588+
test_file_node: TestNode = file_nodes_dict[os.fspath(parent_path)]
586589
except KeyError:
587-
test_file_node = create_file_node(parent_module)
588-
file_nodes_dict[parent_module] = test_file_node
590+
test_file_node = create_file_node(parent_path)
591+
file_nodes_dict[os.fspath(parent_path)] = test_file_node
589592
# Check if the class is already a child of the file node.
590593
if test_class_node is not None and test_class_node not in test_file_node["children"]:
591594
test_file_node["children"].append(test_class_node)
592595
elif not hasattr(test_case, "callspec"):
593596
# This includes test cases that are pytest functions or a doctests.
597+
parent_path = get_node_path(test_case.parent)
594598
try:
595-
parent_test_case = file_nodes_dict[test_case.parent]
599+
parent_test_case = file_nodes_dict[os.fspath(parent_path)]
596600
except KeyError:
597-
parent_test_case = create_file_node(test_case.parent)
598-
file_nodes_dict[test_case.parent] = parent_test_case
601+
parent_test_case = create_file_node(parent_path)
602+
file_nodes_dict[os.fspath(parent_path)] = parent_test_case
599603
parent_test_case["children"].append(test_node)
600604
created_files_folders_dict: dict[str, TestNode] = {}
601605
for file_node in file_nodes_dict.values():
@@ -753,18 +757,17 @@ def create_parameterized_function_node(
753757
}
754758

755759

756-
def create_file_node(file_module: Any) -> TestNode:
757-
"""Creates a file node from a pytest file module.
760+
def create_file_node(calculated_node_path: pathlib.Path) -> TestNode:
761+
"""Creates a file node from a path which has already been calculated using the get_node_path function.
758762
759763
Keyword arguments:
760-
file_module -- the pytest file module.
764+
calculated_node_path -- the pytest file path.
761765
"""
762-
node_path = get_node_path(file_module)
763766
return {
764-
"name": node_path.name,
765-
"path": node_path,
767+
"name": calculated_node_path.name,
768+
"path": calculated_node_path,
766769
"type_": "file",
767-
"id_": os.fspath(node_path),
770+
"id_": os.fspath(calculated_node_path),
768771
"children": [],
769772
}
770773

0 commit comments

Comments
 (0)