diff --git a/AUTHORS b/AUTHORS index d1f1b5503..f91b31e7f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -98,6 +98,7 @@ David Szotten David Vierra Daw-Ran Liou Debi Mishra +Delgan Denis Kirisov Denivy Braiam Rück Dhiren Serai diff --git a/changelog/9619.feature.rst b/changelog/9619.feature.rst new file mode 100644 index 000000000..55ac43c1e --- /dev/null +++ b/changelog/9619.feature.rst @@ -0,0 +1 @@ +Added a new ``pytest_modify_args`` hook. diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index 4ea391be2..a1ff85ab1 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -627,6 +627,8 @@ Initialization hooks Initialization hooks called for plugins and ``conftest.py`` files. +.. hook:: pytest_modify_args +.. autofunction:: pytest_modify_args .. hook:: pytest_addoption .. autofunction:: pytest_addoption .. hook:: pytest_addhooks diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index b3173d7d7..4c75ee776 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1318,6 +1318,7 @@ class Config: kwargs=dict(pluginmanager=self.pluginmanager) ) self._preparse(args, addopts=addopts) + self.hook.pytest_modify_args(args=args) # XXX deprecated hook: self.hook.pytest_cmdline_preparse(config=self, args=args) self._parser.after_preparse = True # type: ignore diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index a03c0e9ab..84efd515e 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -172,6 +172,13 @@ def pytest_cmdline_preparse(config: "Config", args: List[str]) -> None: """ +def pytest_modify_args(args: List[str]) -> None: + """Modify command line arguments before option parsing. + + :param List[str] args: Arguments passed on the command line. + """ + + @hookspec(firstresult=True) def pytest_cmdline_main(config: "Config") -> Optional[Union["ExitCode", int]]: """Called for performing the main command line action. The default diff --git a/testing/test_config.py b/testing/test_config.py index c2e3fe5bb..63c884f5c 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1296,6 +1296,17 @@ def test_load_initial_conftest_last_ordering(_config_for_test): ] +def test_modify_args_hook(pytester: Pytester) -> None: + pytester.makeconftest( + """ + def pytest_modify_args(args): + args.append("-h") + """ + ) + result = pytester.runpytest() + result.stdout.fnmatch_lines(["*pytest*", "*-h*"]) + + def test_get_plugin_specs_as_list() -> None: def exp_match(val: object) -> str: return (