Skip to content

Commit 0ac069d

Browse files
authored
Merge pull request #5006 from blueyed/capture-clean
tests: ensure cleanup with configs via get_config()
2 parents aae0286 + d17ea7a commit 0ac069d

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

src/_pytest/pytester.py

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

377377

378+
@pytest.fixture
379+
def _config_for_test():
380+
from _pytest.config import get_config
381+
382+
config = get_config()
383+
yield config
384+
config._ensure_unconfigure() # cleanup, e.g. capman closing tmpfiles.
385+
386+
378387
rex_outcome = re.compile(r"(\d+) ([\w-]+)")
379388

380389

testing/test_config.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,8 @@ def pytest_internalerror(self, excrepr):
743743
assert not err
744744

745745

746-
def test_load_initial_conftest_last_ordering(testdir):
747-
from _pytest.config import get_config
748-
749-
pm = get_config().pluginmanager
746+
def test_load_initial_conftest_last_ordering(testdir, _config_for_test):
747+
pm = _config_for_test.pluginmanager
750748

751749
class My(object):
752750
def pytest_load_initial_conftests(self):
@@ -1018,21 +1016,17 @@ def test_with_existing_file_in_subdir(self, tmpdir):
10181016
assert rootdir == tmpdir
10191017
assert inifile is None
10201018

1021-
def test_addopts_before_initini(self, monkeypatch):
1019+
def test_addopts_before_initini(self, monkeypatch, _config_for_test):
10221020
cache_dir = ".custom_cache"
10231021
monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir)
1024-
from _pytest.config import get_config
1025-
1026-
config = get_config()
1022+
config = _config_for_test
10271023
config._preparse([], addopts=True)
10281024
assert config._override_ini == ["cache_dir=%s" % cache_dir]
10291025

1030-
def test_addopts_from_env_not_concatenated(self, monkeypatch):
1026+
def test_addopts_from_env_not_concatenated(self, monkeypatch, _config_for_test):
10311027
"""PYTEST_ADDOPTS should not take values from normal args (#4265)."""
1032-
from _pytest.config import get_config
1033-
10341028
monkeypatch.setenv("PYTEST_ADDOPTS", "-o")
1035-
config = get_config()
1029+
config = _config_for_test
10361030
with pytest.raises(UsageError) as excinfo:
10371031
config._preparse(["cache_dir=ignored"], addopts=True)
10381032
assert (
@@ -1057,11 +1051,9 @@ def test_addopts_from_ini_not_concatenated(self, testdir):
10571051
)
10581052
assert result.ret == _pytest.main.EXIT_USAGEERROR
10591053

1060-
def test_override_ini_does_not_contain_paths(self):
1054+
def test_override_ini_does_not_contain_paths(self, _config_for_test):
10611055
"""Check that -o no longer swallows all options after it (#3103)"""
1062-
from _pytest.config import get_config
1063-
1064-
config = get_config()
1056+
config = _config_for_test
10651057
config._preparse(["-o", "cache_dir=/cache", "/some/test/path"])
10661058
assert config._override_ini == ["cache_dir=/cache"]
10671059

testing/test_pluginmanager.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import types
1010

1111
import pytest
12-
from _pytest.config import get_config
1312
from _pytest.config import PytestPluginManager
1413
from _pytest.main import EXIT_NOTESTSCOLLECTED
1514
from _pytest.main import Session
@@ -21,7 +20,7 @@ def pytestpm():
2120

2221

2322
class TestPytestPluginInteractions(object):
24-
def test_addhooks_conftestplugin(self, testdir):
23+
def test_addhooks_conftestplugin(self, testdir, _config_for_test):
2524
testdir.makepyfile(
2625
newhooks="""
2726
def pytest_myhook(xyz):
@@ -37,7 +36,7 @@ def pytest_myhook(xyz):
3736
return xyz + 1
3837
"""
3938
)
40-
config = get_config()
39+
config = _config_for_test
4140
pm = config.pluginmanager
4241
pm.hook.pytest_addhooks.call_historic(
4342
kwargs=dict(pluginmanager=config.pluginmanager)
@@ -92,8 +91,8 @@ def pytest_configure(self, config):
9291
config.pluginmanager.register(A())
9392
assert len(values) == 2
9493

95-
def test_hook_tracing(self):
96-
pytestpm = get_config().pluginmanager # fully initialized with plugins
94+
def test_hook_tracing(self, _config_for_test):
95+
pytestpm = _config_for_test.pluginmanager # fully initialized with plugins
9796
saveindent = []
9897

9998
class api1(object):
@@ -202,8 +201,8 @@ def test_consider_module(self, testdir, pytestpm):
202201
assert pytestpm.get_plugin("pytest_p1").__name__ == "pytest_p1"
203202
assert pytestpm.get_plugin("pytest_p2").__name__ == "pytest_p2"
204203

205-
def test_consider_module_import_module(self, testdir):
206-
pytestpm = get_config().pluginmanager
204+
def test_consider_module_import_module(self, testdir, _config_for_test):
205+
pytestpm = _config_for_test.pluginmanager
207206
mod = types.ModuleType("x")
208207
mod.pytest_plugins = "pytest_a"
209208
aplugin = testdir.makepyfile(pytest_a="#")

0 commit comments

Comments
 (0)