-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add returncode argument to pytest.exit #4145
Conversation
If the argument is not None, it'll raise a SystemExit exception to cleanly exit pytest.
It checks that a SystemError was raised and the SystemError code is the same as the returncode argument.
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.
Thanks @cacoze for the PR!
Please take a look at my comments. 👍
src/_pytest/outcomes.py
Outdated
if returncode: | ||
raise SystemExit(returncode) | ||
else: | ||
raise Exit(msg) |
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 we should always raise Exit
, but pass the argument along to it: Exit(msg, returncode)
, and treat it accordingly in main.py
:
Lines 185 to 188 in 7e1fac5
if initstate < 2 and isinstance(excinfo.value, exit.Exception): | |
sys.stderr.write("{}: {}\n".format(excinfo.typename, excinfo.value.msg)) | |
config.hook.pytest_keyboard_interrupt(excinfo=excinfo) | |
session.exitstatus = EXIT_INTERRUPTED |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please update the docstring for pytest.exit
testing/test_runner.py
Outdated
@@ -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 comment
The 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 testdir
, something like:
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
changelog/4098.feature.rst
Outdated
@@ -0,0 +1 @@ | |||
Add to pytest.exit a returncode argument to cleanly exit pytest. |
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:
Add
returncode
argument topytest.exit()
to exit pytest with a specific return code.
Codecov Report
@@ Coverage Diff @@
## features #4145 +/- ##
============================================
+ Coverage 94.45% 94.49% +0.04%
============================================
Files 109 109
Lines 24169 24177 +8
Branches 2381 2382 +1
============================================
+ Hits 22829 22847 +18
+ Misses 1021 1015 -6
+ Partials 319 315 -4
Continue to review full report at Codecov.
|
Thanks for the feedback @nicoddemus . I'm still learning about the project and had no idea that I could use _pytest/main.py to set the return code from the test. |
src/_pytest/outcomes.py
Outdated
@@ -49,18 +49,23 @@ class Failed(OutcomeException): | |||
class Exit(KeyboardInterrupt): | |||
""" raised for immediate program exits (no tracebacks/summaries)""" | |||
|
|||
def __init__(self, msg="unknown reason"): | |||
def __init__(self, returncode=None, msg="unknown reason"): |
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.
Please add returncode
to the end of the parameter list to keep backward compatibility; technically users should not raise this explicitly, but it doesn't hurt to keep compatibility in this case.
src/_pytest/outcomes.py
Outdated
""" | ||
Exit testing process as if KeyboardInterrupt was triggered. | ||
|
||
:param int returncode: return code to be used when exiting pytest.. |
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.
While we're at it, please add a description for the msg
parameter as well:
:param str msg: message to display upon exit.
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.
Also there's an extra period at the end of this sentence.
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.
Great job! 😁
Just a btw from reading commits on master: squashing this would have made a lot of sense. |
Solves #4098