From 046aa0b6e93db59dbb67f6909415215d038fd02b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 21 Oct 2019 23:45:57 +0200 Subject: [PATCH] pytest.main: return ExitCode --- changelog/6023.improvement.rst | 1 + src/_pytest/config/__init__.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 changelog/6023.improvement.rst diff --git a/changelog/6023.improvement.rst b/changelog/6023.improvement.rst new file mode 100644 index 000000000..6cf81002e --- /dev/null +++ b/changelog/6023.improvement.rst @@ -0,0 +1 @@ +``pytest.main`` returns a ``pytest.ExitCode`` instance now, except for when custom exit codes are used (where it returns ``int`` then still). diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index ea709a26a..4746fd6c7 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -18,6 +18,7 @@ from typing import Optional from typing import Sequence from typing import Set from typing import Tuple +from typing import Union import attr import py @@ -56,7 +57,7 @@ class ConftestImportFailure(Exception): self.excinfo = excinfo # type: Tuple[Type[Exception], Exception, TracebackType] -def main(args=None, plugins=None): +def main(args=None, plugins=None) -> "Union[int, _pytest.main.ExitCode]": """ return exit code, after performing an in-process test run. :arg args: list of command line arguments. @@ -84,10 +85,16 @@ def main(args=None, plugins=None): formatted_tb = str(exc_repr) for line in formatted_tb.splitlines(): tw.line(line.rstrip(), red=True) - return 4 + return ExitCode.USAGE_ERROR else: try: - return config.hook.pytest_cmdline_main(config=config) + ret = config.hook.pytest_cmdline_main( + config=config + ) # type: Union[ExitCode, int] + try: + return ExitCode(ret) + except ValueError: + return ret finally: config._ensure_unconfigure() except UsageError as e: