Skip to content

Commit 046aa0b

Browse files
committed
pytest.main: return ExitCode
1 parent f0c2b07 commit 046aa0b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

changelog/6023.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``pytest.main`` returns a ``pytest.ExitCode`` instance now, except for when custom exit codes are used (where it returns ``int`` then still).

src/_pytest/config/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Sequence
1919
from typing import Set
2020
from typing import Tuple
21+
from typing import Union
2122

2223
import attr
2324
import py
@@ -56,7 +57,7 @@ def __init__(self, path, excinfo):
5657
self.excinfo = excinfo # type: Tuple[Type[Exception], Exception, TracebackType]
5758

5859

59-
def main(args=None, plugins=None):
60+
def main(args=None, plugins=None) -> "Union[int, _pytest.main.ExitCode]":
6061
""" return exit code, after performing an in-process test run.
6162
6263
:arg args: list of command line arguments.
@@ -84,10 +85,16 @@ def main(args=None, plugins=None):
8485
formatted_tb = str(exc_repr)
8586
for line in formatted_tb.splitlines():
8687
tw.line(line.rstrip(), red=True)
87-
return 4
88+
return ExitCode.USAGE_ERROR
8889
else:
8990
try:
90-
return config.hook.pytest_cmdline_main(config=config)
91+
ret = config.hook.pytest_cmdline_main(
92+
config=config
93+
) # type: Union[ExitCode, int]
94+
try:
95+
return ExitCode(ret)
96+
except ValueError:
97+
return ret
9198
finally:
9299
config._ensure_unconfigure()
93100
except UsageError as e:

0 commit comments

Comments
 (0)