Add returncode argument to pytest.exit#4145
Add returncode argument to pytest.exit#4145crazymerlyn merged 12 commits intopytest-dev:featuresfrom
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.
nicoddemus
left a comment
There was a problem hiding this comment.
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.
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
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 = exitstatusThere was a problem hiding this comment.
Also, please update the docstring for pytest.exit
testing/test_runner.py
Outdated
| result.stderr.fnmatch_lines(["Exit: oh noes"]) | ||
|
|
||
|
|
||
| def test_pytest_exit_returncode(): |
There was a problem hiding this comment.
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.
I think this reads better:
Add
returncodeargument 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
| """ 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.
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.
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.
Also there's an extra period at the end of this sentence.
|
Just a btw from reading commits on master: squashing this would have made a lot of sense. |
Solves #4098