@@ -547,6 +547,8 @@ def _set_initial_conftests(
547
547
confcutdir : Optional [Path ],
548
548
invocation_dir : Path ,
549
549
importmode : Union [ImportMode , str ],
550
+ * ,
551
+ consider_namespace_packages : bool ,
550
552
) -> None :
551
553
"""Load initial conftest files given a preparsed "namespace".
552
554
@@ -572,10 +574,20 @@ def _set_initial_conftests(
572
574
# Ensure we do not break if what appears to be an anchor
573
575
# is in fact a very long option (#10169, #11394).
574
576
if safe_exists (anchor ):
575
- self ._try_load_conftest (anchor , importmode , rootpath )
577
+ self ._try_load_conftest (
578
+ anchor ,
579
+ importmode ,
580
+ rootpath ,
581
+ consider_namespace_packages = consider_namespace_packages ,
582
+ )
576
583
foundanchor = True
577
584
if not foundanchor :
578
- self ._try_load_conftest (invocation_dir , importmode , rootpath )
585
+ self ._try_load_conftest (
586
+ invocation_dir ,
587
+ importmode ,
588
+ rootpath ,
589
+ consider_namespace_packages = consider_namespace_packages ,
590
+ )
579
591
580
592
def _is_in_confcutdir (self , path : Path ) -> bool :
581
593
"""Whether to consider the given path to load conftests from."""
@@ -593,17 +605,37 @@ def _is_in_confcutdir(self, path: Path) -> bool:
593
605
return path not in self ._confcutdir .parents
594
606
595
607
def _try_load_conftest (
596
- self , anchor : Path , importmode : Union [str , ImportMode ], rootpath : Path
608
+ self ,
609
+ anchor : Path ,
610
+ importmode : Union [str , ImportMode ],
611
+ rootpath : Path ,
612
+ * ,
613
+ consider_namespace_packages : bool ,
597
614
) -> None :
598
- self ._loadconftestmodules (anchor , importmode , rootpath )
615
+ self ._loadconftestmodules (
616
+ anchor ,
617
+ importmode ,
618
+ rootpath ,
619
+ consider_namespace_packages = consider_namespace_packages ,
620
+ )
599
621
# let's also consider test* subdirs
600
622
if anchor .is_dir ():
601
623
for x in anchor .glob ("test*" ):
602
624
if x .is_dir ():
603
- self ._loadconftestmodules (x , importmode , rootpath )
625
+ self ._loadconftestmodules (
626
+ x ,
627
+ importmode ,
628
+ rootpath ,
629
+ consider_namespace_packages = consider_namespace_packages ,
630
+ )
604
631
605
632
def _loadconftestmodules (
606
- self , path : Path , importmode : Union [str , ImportMode ], rootpath : Path
633
+ self ,
634
+ path : Path ,
635
+ importmode : Union [str , ImportMode ],
636
+ rootpath : Path ,
637
+ * ,
638
+ consider_namespace_packages : bool ,
607
639
) -> None :
608
640
if self ._noconftest :
609
641
return
@@ -620,7 +652,12 @@ def _loadconftestmodules(
620
652
if self ._is_in_confcutdir (parent ):
621
653
conftestpath = parent / "conftest.py"
622
654
if conftestpath .is_file ():
623
- mod = self ._importconftest (conftestpath , importmode , rootpath )
655
+ mod = self ._importconftest (
656
+ conftestpath ,
657
+ importmode ,
658
+ rootpath ,
659
+ consider_namespace_packages = consider_namespace_packages ,
660
+ )
624
661
clist .append (mod )
625
662
self ._dirpath2confmods [directory ] = clist
626
663
@@ -642,7 +679,12 @@ def _rget_with_confmod(
642
679
raise KeyError (name )
643
680
644
681
def _importconftest (
645
- self , conftestpath : Path , importmode : Union [str , ImportMode ], rootpath : Path
682
+ self ,
683
+ conftestpath : Path ,
684
+ importmode : Union [str , ImportMode ],
685
+ rootpath : Path ,
686
+ * ,
687
+ consider_namespace_packages : bool ,
646
688
) -> types .ModuleType :
647
689
conftestpath_plugin_name = str (conftestpath )
648
690
existing = self .get_plugin (conftestpath_plugin_name )
@@ -661,7 +703,12 @@ def _importconftest(
661
703
pass
662
704
663
705
try :
664
- mod = import_path (conftestpath , mode = importmode , root = rootpath )
706
+ mod = import_path (
707
+ conftestpath ,
708
+ mode = importmode ,
709
+ root = rootpath ,
710
+ consider_namespace_packages = consider_namespace_packages ,
711
+ )
665
712
except Exception as e :
666
713
assert e .__traceback__ is not None
667
714
raise ConftestImportFailure (conftestpath , cause = e ) from e
@@ -1177,6 +1224,9 @@ def pytest_load_initial_conftests(self, early_config: "Config") -> None:
1177
1224
confcutdir = early_config .known_args_namespace .confcutdir ,
1178
1225
invocation_dir = early_config .invocation_params .dir ,
1179
1226
importmode = early_config .known_args_namespace .importmode ,
1227
+ consider_namespace_packages = early_config .getini (
1228
+ "consider_namespace_packages"
1229
+ ),
1180
1230
)
1181
1231
1182
1232
def _initini (self , args : Sequence [str ]) -> None :
0 commit comments