From e880a685eb0c36b3e68ad44f6cbc0e68f3927316 Mon Sep 17 00:00:00 2001 From: Sripradha Karkala Date: Sat, 26 Jun 2021 17:50:36 -0700 Subject: [PATCH 1/3] doc: Adding exit code for pytest usage examples (#8790) --- doc/en/how-to/usage.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/en/how-to/usage.rst b/doc/en/how-to/usage.rst index 517c504df7a..abee58eac6e 100644 --- a/doc/en/how-to/usage.rst +++ b/doc/en/how-to/usage.rst @@ -168,7 +168,8 @@ You can invoke ``pytest`` from Python code directly: .. code-block:: python - pytest.main() + if __name__ == "__main__": + sys.exit(pytest.main()) this acts as if you would call "pytest" from the command line. It will not raise ``SystemExit`` but return the exitcode instead. @@ -176,7 +177,8 @@ You can pass in options and arguments: .. code-block:: python - pytest.main(["-x", "mytestdir"]) + if __name__ == "__main__": + sys.exit(pytest.main(["-x", "mytestdir"])) You can specify additional plugins to ``pytest.main``: @@ -191,7 +193,8 @@ You can specify additional plugins to ``pytest.main``: print("*** test run reporting finishing") - pytest.main(["-qq"], plugins=[MyPlugin()]) + if __name__ == "__main__": + sys.exit(pytest.main(["-qq"], plugins=[MyPlugin()])) Running it will show that ``MyPlugin`` was added and its hook was invoked: From 2c3021978d2d7dbb40da5614aeb84fe02fc4fcb9 Mon Sep 17 00:00:00 2001 From: Sripradha Karkala Date: Sat, 26 Jun 2021 21:15:17 -0700 Subject: [PATCH 2/3] doc: Addressing review comments --- doc/en/how-to/usage.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/doc/en/how-to/usage.rst b/doc/en/how-to/usage.rst index abee58eac6e..2175b6b56cd 100644 --- a/doc/en/how-to/usage.rst +++ b/doc/en/how-to/usage.rst @@ -168,8 +168,7 @@ You can invoke ``pytest`` from Python code directly: .. code-block:: python - if __name__ == "__main__": - sys.exit(pytest.main()) + retcode = pytest.main() this acts as if you would call "pytest" from the command line. It will not raise ``SystemExit`` but return the exitcode instead. @@ -177,8 +176,7 @@ You can pass in options and arguments: .. code-block:: python - if __name__ == "__main__": - sys.exit(pytest.main(["-x", "mytestdir"])) + retcode = pytest.main(["-x", "mytestdir"]) You can specify additional plugins to ``pytest.main``: From af6d836aa77b95cb2e42b20868cddd1e5e16c247 Mon Sep 17 00:00:00 2001 From: Srip Date: Sat, 26 Jun 2021 21:18:28 -0700 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Bruno Oliveira --- doc/en/how-to/usage.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/how-to/usage.rst b/doc/en/how-to/usage.rst index 2175b6b56cd..ce8247b44ee 100644 --- a/doc/en/how-to/usage.rst +++ b/doc/en/how-to/usage.rst @@ -171,7 +171,7 @@ You can invoke ``pytest`` from Python code directly: retcode = pytest.main() this acts as if you would call "pytest" from the command line. -It will not raise ``SystemExit`` but return the exitcode instead. +It will not raise :class:`SystemExit` but return the :ref:`exit code ` instead. You can pass in options and arguments: .. code-block:: python