1313_ARGUMENTS = 'cpp_arguments'
1414
1515
16+ # pytest 5.4 introduced the 'from_parent' constructor
17+ needs_from_parent = hasattr (pytest .Item , "from_parent" )
18+
19+
1620def pytest_collect_file (parent , path ):
1721 try :
1822 is_executable = os .stat (str (path )).st_mode & stat .S_IXUSR
@@ -38,7 +42,10 @@ def pytest_collect_file(parent, path):
3842 return
3943 for facade_class in FACADES :
4044 if facade_class .is_test_suite (str (path )):
41- return CppFile (path , parent , facade_class (), test_args )
45+ if needs_from_parent :
46+ return CppFile .from_parent (fspath = path , parent = parent , facade = facade_class (), arguments = test_args )
47+ else :
48+ return CppFile (path , parent , facade_class (), test_args )
4249
4350
4451def pytest_addoption (parser ):
@@ -57,22 +64,35 @@ def pytest_addoption(parser):
5764
5865
5966class CppFile (pytest .File ):
60- def __init__ (self , path , parent , facade , arguments ):
61- pytest .File .__init__ (self , path , parent )
67+ def __init__ (self , fspath , parent , facade , arguments ):
68+ pytest .File .__init__ (self , fspath , parent )
6269 self .facade = facade
6370 self ._arguments = arguments
6471
72+ @classmethod
73+ def from_parent (cls , parent , fspath , facade , arguments ):
74+ # TODO: after dropping python 2, change to keyword only after 'parent'
75+ return super ().from_parent (parent = parent , fspath = fspath , facade = facade , arguments = arguments )
76+
6577 def collect (self ):
6678 for test_id in self .facade .list_tests (str (self .fspath )):
67- yield CppItem (test_id , self , self .facade , self ._arguments )
79+ if needs_from_parent :
80+ yield CppItem .from_parent (parent = self , name = test_id , facade = self .facade , arguments = self ._arguments )
81+ else :
82+ yield CppItem (test_id , self , self .facade , self ._arguments )
6883
6984
7085class CppItem (pytest .Item ):
71- def __init__ (self , name , collector , facade , arguments ):
72- pytest .Item .__init__ (self , name , collector )
86+ def __init__ (self , name , parent , facade , arguments ):
87+ pytest .Item .__init__ (self , name , parent )
7388 self .facade = facade
7489 self ._arguments = arguments
7590
91+ @classmethod
92+ def from_parent (cls , parent , name , facade , arguments ):
93+ # TODO: after dropping python 2, change to keyword only after 'parent'
94+ return super ().from_parent (name = name , parent = parent , facade = facade , arguments = arguments )
95+
7696 def runtest (self ):
7797 failures = self .facade .run_test (str (self .fspath ),
7898 self .name ,
0 commit comments