-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add returncode argument to pytest.exit #4145
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
Changes from 3 commits
d7be039
ce55dcf
a066635
46d6a3f
836c9f8
766d2da
d4dfd52
bbd1cbb
d32f2c5
76fb997
40091ec
27d932e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add to pytest.exit a returncode argument to cleanly exit pytest. | ||
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -57,10 +57,13 @@ def __init__(self, msg="unknown reason"): | |||||||||
# exposed helper methods | ||||||||||
|
||||||||||
|
||||||||||
def exit(msg): | ||||||||||
def exit(msg, returncode=None): | ||||||||||
nicoddemus marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
""" exit testing process as if KeyboardInterrupt was triggered. """ | ||||||||||
__tracebackhide__ = True | ||||||||||
raise Exit(msg) | ||||||||||
if returncode: | ||||||||||
raise SystemExit(returncode) | ||||||||||
else: | ||||||||||
raise Exit(msg) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should always raise Lines 185 to 188 in 7e1fac5
Like this: existstatus = EXIT_INTERRUPTED
if initstate < 2 and isinstance(excinfo.value, exit.Exception):
sys.stderr.write("{}: {}\n".format(excinfo.typename, excinfo.value.msg))
exitstatus = excinfo.value.exitstatus
config.hook.pytest_keyboard_interrupt(excinfo=excinfo)
session.exitstatus = exitstatus There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, please update the docstring for |
||||||||||
|
||||||||||
|
||||||||||
exit.Exception = Exit | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -570,6 +570,15 @@ def pytest_configure(config): | |
result.stderr.fnmatch_lines(["Exit: oh noes"]) | ||
|
||
|
||
def test_pytest_exit_returncode(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following my suggestion above, this will need to be changed to a test using def test_pytest_exit_returncode(testdir):
testdir.makepyfile("""
import pytest
def test_foo():
pytest.exit("some exit msg", 99)
""")
result = testdir.runpytest()
assert result.ret == 99 |
||
try: | ||
pytest.exit("hello", returncode=2) | ||
except SystemExit as exc: | ||
excinfo = _pytest._code.ExceptionInfo() | ||
assert excinfo.errisinstance(SystemExit) | ||
assert excinfo.value.code == 2 | ||
|
||
|
||
def test_pytest_fail_notrace_runtest(testdir): | ||
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during test run.""" | ||
testdir.makepyfile( | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this reads better: