From 6573107b97e96c327db282d45623061edb5afddd Mon Sep 17 00:00:00 2001 From: Srip Date: Sun, 27 Jun 2021 06:29:16 -0700 Subject: [PATCH] doc: Adding exit code for pytest usage examples (#8790) (#8800) * doc: Adding exit code for pytest usage examples (#8790) Fix #8790 Co-authored-by: Bruno Oliveira Co-authored-by: Sripradha Karkala --- doc/en/how-to/usage.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/en/how-to/usage.rst b/doc/en/how-to/usage.rst index 517c504df..ce8247b44 100644 --- a/doc/en/how-to/usage.rst +++ b/doc/en/how-to/usage.rst @@ -168,15 +168,15 @@ You can invoke ``pytest`` from Python code directly: .. code-block:: python - 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. +It will not raise :class:`SystemExit` but return the :ref:`exit code ` instead. You can pass in options and arguments: .. code-block:: python - pytest.main(["-x", "mytestdir"]) + retcode = pytest.main(["-x", "mytestdir"]) You can specify additional plugins to ``pytest.main``: @@ -191,7 +191,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: