Skip to content

Commit 415899d

Browse files
committed
config: handle -p no:plugin with default plugins
`-p no:capture` should not load its fixtures in the first place.
1 parent da81c1e commit 415899d

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

changelog/4957.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``-p no:plugin`` is handled correctly for default (internal) plugins now, e.g. with ``-p no:capture``.
2+
3+
Previously they were loaded (imported) always, making e.g. the ``capfd`` fixture available.

src/_pytest/config/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,15 @@ def directory_arg(path, optname):
147147
builtin_plugins.add("pytester")
148148

149149

150-
def get_config():
150+
def get_config(args=None):
151151
# subsequent calls to main will create a fresh instance
152152
pluginmanager = PytestPluginManager()
153153
config = Config(pluginmanager)
154+
155+
if args is not None:
156+
# Handle any "-p no:plugin" args.
157+
pluginmanager.consider_preparse(args)
158+
154159
for spec in default_plugins:
155160
pluginmanager.import_plugin(spec)
156161
return config
@@ -178,7 +183,7 @@ def _prepareconfig(args=None, plugins=None):
178183
msg = "`args` parameter expected to be a list or tuple of strings, got: {!r} (type: {})"
179184
raise TypeError(msg.format(args, type(args)))
180185

181-
config = get_config()
186+
config = get_config(args)
182187
pluginmanager = config.pluginmanager
183188
try:
184189
if plugins:
@@ -713,7 +718,7 @@ def cwd_relative_nodeid(self, nodeid):
713718
@classmethod
714719
def fromdictargs(cls, option_dict, args):
715720
""" constructor useable for subprocesses. """
716-
config = get_config()
721+
config = get_config(args)
717722
config.option.__dict__.update(option_dict)
718723
config.parse(args, addopts=False)
719724
for x in config.option.plugins:

testing/test_config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from _pytest.config.findpaths import get_common_ancestor
1616
from _pytest.config.findpaths import getcfg
1717
from _pytest.main import EXIT_NOTESTSCOLLECTED
18+
from _pytest.main import EXIT_TESTSFAILED
1819
from _pytest.main import EXIT_USAGEERROR
1920

2021

@@ -1176,3 +1177,15 @@ def pytest_addoption(parser):
11761177
["*pytest*{}*imported from*".format(pytest.__version__)]
11771178
)
11781179
assert result.ret == EXIT_USAGEERROR
1180+
1181+
1182+
def test_config_does_not_load_blocked_plugin_from_args(testdir):
1183+
"""This tests that pytest's config setup handles "-p no:X"."""
1184+
p = testdir.makepyfile("def test(capfd): pass")
1185+
result = testdir.runpytest(str(p), "-pno:capture", "--tb=native")
1186+
result.stdout.fnmatch_lines(["E fixture 'capfd' not found"])
1187+
assert result.ret == EXIT_TESTSFAILED
1188+
1189+
result = testdir.runpytest(str(p), "-pno:capture", "-s")
1190+
result.stderr.fnmatch_lines(["*: error: unrecognized arguments: -s"])
1191+
assert result.ret == EXIT_USAGEERROR

0 commit comments

Comments
 (0)