Skip to content

Commit 3e669a2

Browse files
committed
Introduce Config.invocation_args and Config.invocation_plugins
These attributes can be used to access the unchanged arguments passed to pytest.main(). The intention is to use these attributes to initialize workers in the same manner as the master node is initialized in pytest-xdist.
1 parent 60a358f commit 3e669a2

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

changelog/5564.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
New ``Config.invocation_args`` and ``Config.invocation_plugins`` attributes.
2+
3+
These attributes can be used by plugins to access the unchanged arguments passed to ``pytest.main()``.

src/_pytest/config/__init__.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def main(args=None, plugins=None):
7070
tw.line(line.rstrip(), red=True)
7171
return 4
7272
else:
73+
config.invocation_args = args
74+
config.invocation_plugins = plugins
7375
try:
7476
return config.hook.pytest_cmdline_main(config=config)
7577
finally:
@@ -608,20 +610,33 @@ def _iter_rewritable_modules(package_files):
608610

609611

610612
class Config:
611-
""" access to configuration values, pluginmanager and plugin hooks. """
613+
"""
614+
Access to configuration values, pluginmanager and plugin hooks.
615+
616+
:ivar PytestPluginManager pluginmanager: the plugin manager handles plugin registration and hook invocation.
617+
618+
:ivar argparse.Namespace option: access to command line option as attributes.
619+
620+
:ivar invocation_args: list of command-line arguments as passed to pytest.main()
621+
622+
:ivar invocation_plugins: list of extra plugins passed to pytest.main(), might be None
623+
624+
:ivar py.path.local invocation_dir: directory where pytest.main() was invoked from
625+
"""
612626

613627
def __init__(self, pluginmanager):
614-
#: access to command line option as attributes.
615-
#: (deprecated), use :py:func:`getoption() <_pytest.config.Config.getoption>` instead
616-
self.option = argparse.Namespace()
617628
from .argparsing import Parser, FILE_OR_DIR
618629

630+
self.option = argparse.Namespace()
631+
self.invocation_args = None
632+
self.invocation_plugins = None
633+
self.invocation_dir = py.path.local()
634+
619635
_a = FILE_OR_DIR
620636
self._parser = Parser(
621637
usage="%(prog)s [options] [{}] [{}] [...]".format(_a, _a),
622638
processopt=self._processopt,
623639
)
624-
#: a pluginmanager instance
625640
self.pluginmanager = pluginmanager
626641
self.trace = self.pluginmanager.trace.root.get("config")
627642
self.hook = self.pluginmanager.hook
@@ -631,7 +646,6 @@ def __init__(self, pluginmanager):
631646
self._cleanup = []
632647
self.pluginmanager.register(self, "pytestconfig")
633648
self._configured = False
634-
self.invocation_dir = py.path.local()
635649
self.hook.pytest_addoption.call_historic(kwargs=dict(parser=self._parser))
636650

637651
def add_cleanup(self, func):

testing/test_config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,28 @@ def test_config_does_not_load_blocked_plugin_from_args(testdir):
11981198
assert result.ret == ExitCode.USAGE_ERROR
11991199

12001200

1201+
def test_invocation_arguments(testdir):
1202+
"""Ensure that Config.invocation_* arguments are correctly defined"""
1203+
1204+
class DummyPlugin:
1205+
pass
1206+
1207+
p = testdir.makepyfile("def test(): pass")
1208+
plugin = DummyPlugin()
1209+
rec = testdir.inline_run(p, "-v", plugins=[plugin])
1210+
calls = rec.getcalls("pytest_runtest_protocol")
1211+
assert len(calls) == 1
1212+
call = calls[0]
1213+
config = call.item.config
1214+
1215+
assert config.invocation_args == [p, "-v"]
1216+
1217+
plugins = config.invocation_plugins
1218+
assert len(plugins) == 2
1219+
assert plugins[0] is plugin
1220+
assert type(plugins[1]).__name__ == "Collect" # installed by testdir.inline_run()
1221+
1222+
12011223
@pytest.mark.parametrize(
12021224
"plugin",
12031225
[

0 commit comments

Comments
 (0)