Skip to content

Commit 4e931b2

Browse files
committed
Merge master into features
2 parents 6b5cddc + 2812c08 commit 4e931b2

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

changelog/4974.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update docs for ``pytest_cmdline_parse`` hook to note availability liminations

src/_pytest/hookspec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def pytest_cmdline_parse(pluginmanager, args):
9999
Stops at first non-None result, see :ref:`firstresult`
100100
101101
.. note::
102-
This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
102+
This hook will only be called for plugin classes passed to the ``plugins`` arg when using `pytest.main`_ to
103+
perform an in-process test run.
103104
104105
:param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager
105106
:param list[str] args: list of arguments passed on the command line

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 _config_for_test():
340+
from _pytest.config import get_config
341+
342+
config = get_config()
343+
yield config
344+
config._ensure_unconfigure() # cleanup, e.g. capman closing tmpfiles.
345+
346+
338347
rex_outcome = re.compile(r"(\d+) ([\w-]+)")
339348

340349

testing/test_config.py

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

770770

771-
def test_load_initial_conftest_last_ordering(testdir):
772-
from _pytest.config import get_config
773-
774-
pm = get_config().pluginmanager
771+
def test_load_initial_conftest_last_ordering(testdir, _config_for_test):
772+
pm = _config_for_test.pluginmanager
775773

776774
class My(object):
777775
def pytest_load_initial_conftests(self):
@@ -1043,21 +1041,17 @@ def test_with_existing_file_in_subdir(self, tmpdir):
10431041
assert rootdir == tmpdir
10441042
assert inifile is None
10451043

1046-
def test_addopts_before_initini(self, monkeypatch):
1044+
def test_addopts_before_initini(self, monkeypatch, _config_for_test):
10471045
cache_dir = ".custom_cache"
10481046
monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir)
1049-
from _pytest.config import get_config
1050-
1051-
config = get_config()
1047+
config = _config_for_test
10521048
config._preparse([], addopts=True)
10531049
assert config._override_ini == ["cache_dir=%s" % cache_dir]
10541050

1055-
def test_addopts_from_env_not_concatenated(self, monkeypatch):
1051+
def test_addopts_from_env_not_concatenated(self, monkeypatch, _config_for_test):
10561052
"""PYTEST_ADDOPTS should not take values from normal args (#4265)."""
1057-
from _pytest.config import get_config
1058-
10591053
monkeypatch.setenv("PYTEST_ADDOPTS", "-o")
1060-
config = get_config()
1054+
config = _config_for_test
10611055
with pytest.raises(UsageError) as excinfo:
10621056
config._preparse(["cache_dir=ignored"], addopts=True)
10631057
assert (
@@ -1082,11 +1076,9 @@ def test_addopts_from_ini_not_concatenated(self, testdir):
10821076
)
10831077
assert result.ret == _pytest.main.EXIT_USAGEERROR
10841078

1085-
def test_override_ini_does_not_contain_paths(self):
1079+
def test_override_ini_does_not_contain_paths(self, _config_for_test):
10861080
"""Check that -o no longer swallows all options after it (#3103)"""
1087-
from _pytest.config import get_config
1088-
1089-
config = get_config()
1081+
config = _config_for_test
10901082
config._preparse(["-o", "cache_dir=/cache", "/some/test/path"])
10911083
assert config._override_ini == ["cache_dir=/cache"]
10921084

testing/test_pluginmanager.py

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

1010
import pytest
11-
from _pytest.config import get_config
1211
from _pytest.config import PytestPluginManager
1312
from _pytest.main import EXIT_NOTESTSCOLLECTED
1413
from _pytest.main import Session
@@ -20,7 +19,7 @@ def pytestpm():
2019

2120

2221
class TestPytestPluginInteractions(object):
23-
def test_addhooks_conftestplugin(self, testdir):
22+
def test_addhooks_conftestplugin(self, testdir, _config_for_test):
2423
testdir.makepyfile(
2524
newhooks="""
2625
def pytest_myhook(xyz):
@@ -36,7 +35,7 @@ def pytest_myhook(xyz):
3635
return xyz + 1
3736
"""
3837
)
39-
config = get_config()
38+
config = _config_for_test
4039
pm = config.pluginmanager
4140
pm.hook.pytest_addhooks.call_historic(
4241
kwargs=dict(pluginmanager=config.pluginmanager)
@@ -91,8 +90,8 @@ def pytest_configure(self, config):
9190
config.pluginmanager.register(A())
9291
assert len(values) == 2
9392

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

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

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

0 commit comments

Comments
 (0)