Skip to content

pytest.main: return ExitCode #6023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6023.improvement.rst
Original file line number Diff line number Diff line change
@@ -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).
13 changes: 10 additions & 3 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import Sequence
from typing import Set
from typing import Tuple
from typing import Union

import attr
import py
Expand Down Expand Up @@ -56,7 +57,7 @@ def __init__(self, path, excinfo):
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.
Expand Down Expand Up @@ -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:
Expand Down