Skip to content

Commit 1fc37f5

Browse files
committed
switch to use file path as key in building test tree
1 parent 3ccf984 commit 1fc37f5

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
@@ -500,7 +500,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
500500
"""
501501
session_node = create_session_node(session)
502502
session_children_dict: dict[str, TestNode] = {}
503-
file_nodes_dict: dict[Any, TestNode] = {}
503+
file_nodes_dict: dict[str, TestNode] = {}
504504
class_nodes_dict: dict[str, TestNode] = {}
505505
function_nodes_dict: dict[str, TestNode] = {}
506506

@@ -549,11 +549,13 @@ def build_test_tree(session: pytest.Session) -> TestNode:
549549
function_test_node["children"].append(test_node)
550550
# Check if the parent node of the function is file, if so create/add to this file node.
551551
if isinstance(test_case.parent, pytest.File):
552+
# calculate the parent path of the test case
553+
parent_path = get_node_path(test_case.parent)
552554
try:
553-
parent_test_case = file_nodes_dict[test_case.parent]
555+
parent_test_case = file_nodes_dict[os.fspath(parent_path)]
554556
except KeyError:
555-
parent_test_case = create_file_node(test_case.parent)
556-
file_nodes_dict[test_case.parent] = parent_test_case
557+
parent_test_case = create_file_node(parent_path)
558+
file_nodes_dict[os.fspath(parent_path)] = parent_test_case
557559
if function_test_node not in parent_test_case["children"]:
558560
parent_test_case["children"].append(function_test_node)
559561
# If the parent is not a file, it is a class, add the function node as the test node to handle subsequent nesting.
@@ -585,22 +587,24 @@ def build_test_tree(session: pytest.Session) -> TestNode:
585587
else:
586588
ERRORS.append(f"Test class {case_iter} has no parent")
587589
break
590+
parent_path = get_node_path(parent_module)
588591
# Create a file node that has the last class as a child.
589592
try:
590-
test_file_node: TestNode = file_nodes_dict[parent_module]
593+
test_file_node: TestNode = file_nodes_dict[os.fspath(parent_path)]
591594
except KeyError:
592-
test_file_node = create_file_node(parent_module)
593-
file_nodes_dict[parent_module] = test_file_node
595+
test_file_node = create_file_node(parent_path)
596+
file_nodes_dict[os.fspath(parent_path)] = test_file_node
594597
# Check if the class is already a child of the file node.
595598
if test_class_node is not None and test_class_node not in test_file_node["children"]:
596599
test_file_node["children"].append(test_class_node)
597600
elif not hasattr(test_case, "callspec"):
598601
# This includes test cases that are pytest functions or a doctests.
602+
parent_path = get_node_path(test_case.parent)
599603
try:
600-
parent_test_case = file_nodes_dict[test_case.parent]
604+
parent_test_case = file_nodes_dict[os.fspath(parent_path)]
601605
except KeyError:
602-
parent_test_case = create_file_node(test_case.parent)
603-
file_nodes_dict[test_case.parent] = parent_test_case
606+
parent_test_case = create_file_node(parent_path)
607+
file_nodes_dict[os.fspath(parent_path)] = parent_test_case
604608
parent_test_case["children"].append(test_node)
605609
created_files_folders_dict: dict[str, TestNode] = {}
606610
for file_node in file_nodes_dict.values():
@@ -758,18 +762,17 @@ def create_parameterized_function_node(
758762
}
759763

760764

761-
def create_file_node(file_module: Any) -> TestNode:
762-
"""Creates a file node from a pytest file module.
765+
def create_file_node(calculated_node_path: pathlib.Path) -> TestNode:
766+
"""Creates a file node from a path which has already been calculated using the get_node_path function.
763767
764768
Keyword arguments:
765-
file_module -- the pytest file module.
769+
calculated_node_path -- the pytest file path.
766770
"""
767-
node_path = get_node_path(file_module)
768771
return {
769-
"name": node_path.name,
770-
"path": node_path,
772+
"name": calculated_node_path.name,
773+
"path": calculated_node_path,
771774
"type_": "file",
772-
"id_": os.fspath(node_path),
775+
"id_": os.fspath(calculated_node_path),
773776
"children": [],
774777
}
775778

0 commit comments

Comments
 (0)