From c27ba33eb7350d3626df689210ab6994cc8c705f Mon Sep 17 00:00:00 2001 From: Vojtech Antosik Date: Sun, 9 Jul 2023 15:07:55 +0200 Subject: [PATCH 1/2] Clarify docs for pytest.main default behavior Add explicit mentions of the default behavior to both API reference and how-to section about ways to invoke pytest. --- doc/en/how-to/usage.rst | 4 +++- src/_pytest/config/__init__.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/en/how-to/usage.rst b/doc/en/how-to/usage.rst index 8e2dd86736f..c0d122e6fa8 100644 --- a/doc/en/how-to/usage.rst +++ b/doc/en/how-to/usage.rst @@ -173,7 +173,9 @@ You can invoke ``pytest`` from Python code directly: this acts as if you would call "pytest" from the command line. It will not raise :class:`SystemExit` but return the :ref:`exit code ` instead. -You can pass in options and arguments: +Be careful though, by default (when ``None`` is passed) ``main`` will read arguments +from the command line, which may not be desirable. +You can pass in options and arguments explicitly: .. code-block:: python diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 47651ad9fa4..cd404ed2551 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -137,7 +137,9 @@ def main( ) -> Union[int, ExitCode]: """Perform an in-process test run. - :param args: List of command line arguments. + :param args: + List of command line arguments. If `None` or not given, defaults to reading + arguments directly from the command line. :param plugins: List of plugin objects to be auto-registered during initialization. :returns: An exit code. From f7182ce78ab1efb59e004b15801de10a11f7d1a0 Mon Sep 17 00:00:00 2001 From: antosikv <79337398+antosikv@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:35:33 +0200 Subject: [PATCH 2/2] Slightly reword the docs about default main behavior Explicitly mention reading arguments from `sys.argv` to avoid any confusion. Co-authored-by: Ran Benita --- doc/en/how-to/usage.rst | 3 +-- src/_pytest/config/__init__.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/en/how-to/usage.rst b/doc/en/how-to/usage.rst index c0d122e6fa8..4ec4459a232 100644 --- a/doc/en/how-to/usage.rst +++ b/doc/en/how-to/usage.rst @@ -173,8 +173,7 @@ You can invoke ``pytest`` from Python code directly: this acts as if you would call "pytest" from the command line. It will not raise :class:`SystemExit` but return the :ref:`exit code ` instead. -Be careful though, by default (when ``None`` is passed) ``main`` will read arguments -from the command line, which may not be desirable. +If you don't pass it any arguments, ``main`` reads the arguments from the command line arguments of the process (:data:`sys.argv`), which may be undesirable. You can pass in options and arguments explicitly: .. code-block:: python diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index cd404ed2551..3e38594a859 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -139,7 +139,7 @@ def main( :param args: List of command line arguments. If `None` or not given, defaults to reading - arguments directly from the command line. + arguments directly from the process command line (:data:`sys.argv`). :param plugins: List of plugin objects to be auto-registered during initialization. :returns: An exit code.