1111from _pytest .main import EXIT_NOTESTSCOLLECTED
1212from _pytest .main import EXIT_OK
1313from _pytest .main import EXIT_USAGEERROR
14-
15-
16- @pytest .fixture (scope = "module" , params = ["global" , "inpackage" ])
17- def basedir (request , tmpdir_factory ):
18- tmpdir = tmpdir_factory .mktemp ("basedir" , numbered = True )
19- tmpdir .ensure ("adir/conftest.py" ).write ("a=1 ; Directory = 3" )
20- tmpdir .ensure ("adir/b/conftest.py" ).write ("b=2 ; a = 1.5" )
21- if request .param == "inpackage" :
22- tmpdir .ensure ("adir/__init__.py" )
23- tmpdir .ensure ("adir/b/__init__.py" )
24- return tmpdir
14+ from _pytest .pytester import SysModulesSnapshot
15+ from _pytest .pytester import SysPathsSnapshot
2516
2617
2718def ConftestWithSetinitial (path ):
@@ -42,6 +33,26 @@ def __init__(self):
4233
4334
4435class TestConftestValueAccessGlobal (object ):
36+ @pytest .fixture (scope = "module" , params = ["global" , "inpackage" ])
37+ def basedir (self , request , tmpdir_factory ):
38+ tmpdir = tmpdir_factory .mktemp ("basedir" , numbered = True )
39+ tmpdir .ensure ("adir/conftest.py" ).write ("a=1 ; Directory = 3" )
40+ tmpdir .ensure ("adir/b/conftest.py" ).write ("b=2 ; a = 1.5" )
41+ if request .param == "inpackage" :
42+ tmpdir .ensure ("adir/__init__.py" )
43+ tmpdir .ensure ("adir/b/__init__.py" )
44+
45+ yield tmpdir
46+
47+ @pytest .fixture (autouse = True )
48+ def restore (self ):
49+ """Restore sys.modules to prevent ConftestImportFailure when run randomly."""
50+ snapmods = SysModulesSnapshot ()
51+ snappath = SysPathsSnapshot ()
52+ yield
53+ snapmods .restore ()
54+ snappath .restore ()
55+
4556 def test_basic_init (self , basedir ):
4657 conftest = PytestPluginManager ()
4758 p = basedir .join ("adir" )
0 commit comments