Skip to content

Commit 8011ff5

Browse files
committed
Add _sys_snapshot fixture and use it with more tests
1 parent 899e74a commit 8011ff5

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

src/_pytest/pytester.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ def testdir(request, tmpdir_factory):
335335
return Testdir(request, tmpdir_factory)
336336

337337

338+
@pytest.fixture
339+
def _sys_snapshot():
340+
snappaths = SysPathsSnapshot()
341+
snapmods = SysModulesSnapshot()
342+
yield
343+
snapmods.restore()
344+
snappaths.restore()
345+
346+
338347
@pytest.fixture
339348
def _config_for_test():
340349
from _pytest.config import get_config

testing/acceptance_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def test_foo(invalid_fixture):
485485
["*source code not available*", "E*fixture 'invalid_fixture' not found"]
486486
)
487487

488-
def test_plugins_given_as_strings(self, tmpdir, monkeypatch):
488+
def test_plugins_given_as_strings(self, tmpdir, monkeypatch, _sys_snapshot):
489489
"""test that str values passed to main() as `plugins` arg
490490
are interpreted as module names to be imported and registered.
491491
#855.

testing/code/test_excinfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def test_division_zero():
441441

442442
class TestFormattedExcinfo(object):
443443
@pytest.fixture
444-
def importasmod(self, request):
444+
def importasmod(self, request, _sys_snapshot):
445445
def importasmod(source):
446446
source = textwrap.dedent(source)
447447
tmpdir = request.getfixturevalue("tmpdir")

testing/code/test_source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def g():
410410
assert lines == ["def f():", " def g():", " pass"]
411411

412412

413-
def test_source_of_class_at_eof_without_newline(tmpdir):
413+
def test_source_of_class_at_eof_without_newline(tmpdir, _sys_snapshot):
414414
# this test fails because the implicit inspect.getsource(A) below
415415
# does not return the "x = 1" last line.
416416
source = _pytest._code.Source(

testing/test_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def test_iter_rewritable_modules(self, names, expected):
436436

437437

438438
class TestConfigFromdictargs(object):
439-
def test_basic_behavior(self):
439+
def test_basic_behavior(self, _sys_snapshot):
440440
from _pytest.config import Config
441441

442442
option_dict = {"verbose": 444, "foo": "bar", "capture": "no"}
@@ -450,7 +450,7 @@ def test_basic_behavior(self):
450450
assert config.option.capture == "no"
451451
assert config.args == args
452452

453-
def test_origargs(self):
453+
def test_origargs(self, _sys_snapshot):
454454
"""Show that fromdictargs can handle args in their "orig" format"""
455455
from _pytest.config import Config
456456

@@ -1057,7 +1057,7 @@ def test_with_existing_file_in_subdir(self, tmpdir):
10571057
assert rootdir == tmpdir
10581058
assert inifile is None
10591059

1060-
def test_addopts_before_initini(self, monkeypatch, _config_for_test):
1060+
def test_addopts_before_initini(self, monkeypatch, _config_for_test, _sys_snapshot):
10611061
cache_dir = ".custom_cache"
10621062
monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir)
10631063
config = _config_for_test
@@ -1092,7 +1092,7 @@ def test_addopts_from_ini_not_concatenated(self, testdir):
10921092
)
10931093
assert result.ret == _pytest.main.EXIT_USAGEERROR
10941094

1095-
def test_override_ini_does_not_contain_paths(self, _config_for_test):
1095+
def test_override_ini_does_not_contain_paths(self, _config_for_test, _sys_snapshot):
10961096
"""Check that -o no longer swallows all options after it (#3103)"""
10971097
config = _config_for_test
10981098
config._preparse(["-o", "cache_dir=/cache", "/some/test/path"])

testing/test_conftest.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from _pytest.main import EXIT_NOTESTSCOLLECTED
1212
from _pytest.main import EXIT_OK
1313
from _pytest.main import EXIT_USAGEERROR
14-
from _pytest.pytester import SysModulesSnapshot
15-
from _pytest.pytester import SysPathsSnapshot
1614

1715

1816
def ConftestWithSetinitial(path):
@@ -32,6 +30,7 @@ def __init__(self):
3230
conftest._set_initial_conftests(Namespace())
3331

3432

33+
@pytest.mark.usefixtures("_sys_snapshot")
3534
class TestConftestValueAccessGlobal(object):
3635
@pytest.fixture(scope="module", params=["global", "inpackage"])
3736
def basedir(self, request, tmpdir_factory):
@@ -44,15 +43,6 @@ def basedir(self, request, tmpdir_factory):
4443

4544
yield tmpdir
4645

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-
5646
def test_basic_init(self, basedir):
5747
conftest = PytestPluginManager()
5848
p = basedir.join("adir")
@@ -91,7 +81,7 @@ def test_value_access_with_confmod(self, basedir):
9181
assert path.purebasename.startswith("conftest")
9282

9383

94-
def test_conftest_in_nonpkg_with_init(tmpdir):
84+
def test_conftest_in_nonpkg_with_init(tmpdir, _sys_snapshot):
9585
tmpdir.ensure("adir-1.0/conftest.py").write("a=1 ; Directory = 3")
9686
tmpdir.ensure("adir-1.0/b/conftest.py").write("b=2 ; a = 1.5")
9787
tmpdir.ensure("adir-1.0/b/__init__.py")

0 commit comments

Comments
 (0)