@@ -247,7 +247,9 @@ def runtest(self) -> None:
247
247
# TODO: add a better error message for when someone uses skip and xfail at the same time
248
248
elif self .xfail :
249
249
self .add_marker (pytest .mark .xfail )
250
- suite = self .parent .obj ()
250
+ parent = self .getparent (DataSuiteCollector )
251
+ assert parent is not None , 'Should not happen'
252
+ suite = parent .obj ()
251
253
suite .setup ()
252
254
try :
253
255
suite .run_case (self )
@@ -550,12 +552,12 @@ def pytest_pycollect_makeitem(collector: Any, name: str,
550
552
# The collect method of the returned DataSuiteCollector instance will be called later,
551
553
# with self.obj being obj.
552
554
return DataSuiteCollector .from_parent ( # type: ignore[no-untyped-call]
553
- parent = collector , name = name
555
+ parent = collector , name = name ,
554
556
)
555
557
return None
556
558
557
559
558
- def split_test_cases (parent : 'DataSuiteCollector ' , suite : 'DataSuite' ,
560
+ def split_test_cases (parent : 'DataFileCollector ' , suite : 'DataSuite' ,
559
561
file : str ) -> Iterator ['DataDrivenTestCase' ]:
560
562
"""Iterate over raw test cases in file, at collection time, ignoring sub items.
561
563
@@ -596,7 +598,7 @@ def split_test_cases(parent: 'DataSuiteCollector', suite: 'DataSuite',
596
598
597
599
598
600
class DataSuiteCollector (pytest .Class ):
599
- def collect (self ) -> Iterator [pytest . Item ]:
601
+ def collect (self ) -> Iterator ['DataFileCollector' ]:
600
602
"""Called by pytest on each of the object returned from pytest_pycollect_makeitem"""
601
603
602
604
# obj is the object for which pytest_pycollect_makeitem returned self.
@@ -605,8 +607,32 @@ def collect(self) -> Iterator[pytest.Item]:
605
607
assert os .path .isdir (suite .data_prefix ), \
606
608
'Test data prefix ({}) not set correctly' .format (suite .data_prefix )
607
609
608
- for f in suite .files :
609
- yield from split_test_cases (self , suite , os .path .join (suite .data_prefix , f ))
610
+ for data_file in suite .files :
611
+ yield DataFileCollector .from_parent (parent = self , name = data_file )
612
+
613
+
614
+ class DataFileCollector (pytest .Collector ):
615
+ """Represents a single `.test` data driven test file.
616
+
617
+ More context: https://github.com/python/mypy/issues/11662
618
+ """
619
+ parent : DataSuiteCollector
620
+
621
+ @classmethod # We have to fight with pytest here:
622
+ def from_parent ( # type: ignore[override]
623
+ cls ,
624
+ parent : DataSuiteCollector ,
625
+ * ,
626
+ name : str ,
627
+ ) -> 'DataFileCollector' :
628
+ return super ().from_parent (parent , name = name )
629
+
630
+ def collect (self ) -> Iterator ['DataDrivenTestCase' ]:
631
+ yield from split_test_cases (
632
+ parent = self ,
633
+ suite = self .parent .obj ,
634
+ file = os .path .join (self .parent .obj .data_prefix , self .name ),
635
+ )
610
636
611
637
612
638
def add_test_name_suffix (name : str , suffix : str ) -> str :
0 commit comments