@@ -495,7 +495,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
495
495
"""
496
496
session_node = create_session_node (session )
497
497
session_children_dict : dict [str , TestNode ] = {}
498
- file_nodes_dict : dict [Any , TestNode ] = {}
498
+ file_nodes_dict : dict [str , TestNode ] = {}
499
499
class_nodes_dict : dict [str , TestNode ] = {}
500
500
function_nodes_dict : dict [str , TestNode ] = {}
501
501
@@ -544,11 +544,13 @@ def build_test_tree(session: pytest.Session) -> TestNode:
544
544
function_test_node ["children" ].append (test_node )
545
545
# Check if the parent node of the function is file, if so create/add to this file node.
546
546
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 )
547
549
try :
548
- parent_test_case = file_nodes_dict [test_case . parent ]
550
+ parent_test_case = file_nodes_dict [os . fspath ( parent_path ) ]
549
551
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
552
554
if function_test_node not in parent_test_case ["children" ]:
553
555
parent_test_case ["children" ].append (function_test_node )
554
556
# 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:
580
582
else :
581
583
ERRORS .append (f"Test class { case_iter } has no parent" )
582
584
break
585
+ parent_path = get_node_path (parent_module )
583
586
# Create a file node that has the last class as a child.
584
587
try :
585
- test_file_node : TestNode = file_nodes_dict [parent_module ]
588
+ test_file_node : TestNode = file_nodes_dict [os . fspath ( parent_path ) ]
586
589
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
589
592
# Check if the class is already a child of the file node.
590
593
if test_class_node is not None and test_class_node not in test_file_node ["children" ]:
591
594
test_file_node ["children" ].append (test_class_node )
592
595
elif not hasattr (test_case , "callspec" ):
593
596
# This includes test cases that are pytest functions or a doctests.
597
+ parent_path = get_node_path (test_case .parent )
594
598
try :
595
- parent_test_case = file_nodes_dict [test_case . parent ]
599
+ parent_test_case = file_nodes_dict [os . fspath ( parent_path ) ]
596
600
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
599
603
parent_test_case ["children" ].append (test_node )
600
604
created_files_folders_dict : dict [str , TestNode ] = {}
601
605
for file_node in file_nodes_dict .values ():
@@ -753,18 +757,17 @@ def create_parameterized_function_node(
753
757
}
754
758
755
759
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 .
758
762
759
763
Keyword arguments:
760
- file_module -- the pytest file module .
764
+ calculated_node_path -- the pytest file path .
761
765
"""
762
- node_path = get_node_path (file_module )
763
766
return {
764
- "name" : node_path .name ,
765
- "path" : node_path ,
767
+ "name" : calculated_node_path .name ,
768
+ "path" : calculated_node_path ,
766
769
"type_" : "file" ,
767
- "id_" : os .fspath (node_path ),
770
+ "id_" : os .fspath (calculated_node_path ),
768
771
"children" : [],
769
772
}
770
773
0 commit comments