Skip to content

Commit e7ade06

Browse files
authored
Merge pull request #5011 from blueyed/merge-master-into-features
Merge master into features
2 parents dee520e + 4e931b2 commit e7ade06

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
@@ -784,10 +784,8 @@ def pytest_internalerror(self, excrepr):
784784
assert not err
785785

786786

787-
def test_load_initial_conftest_last_ordering(testdir):
788-
from _pytest.config import get_config
789-
790-
pm = get_config().pluginmanager
787+
def test_load_initial_conftest_last_ordering(testdir, _config_for_test):
788+
pm = _config_for_test.pluginmanager
791789

792790
class My(object):
793791
def pytest_load_initial_conftests(self):
@@ -1059,21 +1057,17 @@ def test_with_existing_file_in_subdir(self, tmpdir):
10591057
assert rootdir == tmpdir
10601058
assert inifile is None
10611059

1062-
def test_addopts_before_initini(self, monkeypatch):
1060+
def test_addopts_before_initini(self, monkeypatch, _config_for_test):
10631061
cache_dir = ".custom_cache"
10641062
monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir)
1065-
from _pytest.config import get_config
1066-
1067-
config = get_config()
1063+
config = _config_for_test
10681064
config._preparse([], addopts=True)
10691065
assert config._override_ini == ["cache_dir=%s" % cache_dir]
10701066

1071-
def test_addopts_from_env_not_concatenated(self, monkeypatch):
1067+
def test_addopts_from_env_not_concatenated(self, monkeypatch, _config_for_test):
10721068
"""PYTEST_ADDOPTS should not take values from normal args (#4265)."""
1073-
from _pytest.config import get_config
1074-
10751069
monkeypatch.setenv("PYTEST_ADDOPTS", "-o")
1076-
config = get_config()
1070+
config = _config_for_test
10771071
with pytest.raises(UsageError) as excinfo:
10781072
config._preparse(["cache_dir=ignored"], addopts=True)
10791073
assert (
@@ -1098,11 +1092,9 @@ def test_addopts_from_ini_not_concatenated(self, testdir):
10981092
)
10991093
assert result.ret == _pytest.main.EXIT_USAGEERROR
11001094

1101-
def test_override_ini_does_not_contain_paths(self):
1095+
def test_override_ini_does_not_contain_paths(self, _config_for_test):
11021096
"""Check that -o no longer swallows all options after it (#3103)"""
1103-
from _pytest.config import get_config
1104-
1105-
config = get_config()
1097+
config = _config_for_test
11061098
config._preparse(["-o", "cache_dir=/cache", "/some/test/path"])
11071099
assert config._override_ini == ["cache_dir=/cache"]
11081100

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)