Skip to content

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

Merged
merged 12 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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/4098.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add to pytest.exit a returncode argument to cleanly exit pytest.
Copy link
Member

@nicoddemus nicoddemus Oct 14, 2018

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 to pytest.exit() to exit pytest with a specific return code.

7 changes: 5 additions & 2 deletions src/_pytest/outcomes.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ def __init__(self, msg="unknown reason"):
# exposed helper methods


def exit(msg):
def exit(msg, returncode=None):
""" exit testing process as if KeyboardInterrupt was triggered. """
__tracebackhide__ = True
raise Exit(msg)
if returncode:
raise SystemExit(returncode)
else:
raise Exit(msg)
Copy link
Member

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:

pytest/src/_pytest/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

Copy link
Member

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



exit.Exception = Exit
Expand Down
9 changes: 9 additions & 0 deletions testing/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ def pytest_configure(config):
result.stderr.fnmatch_lines(["Exit: oh noes"])


def test_pytest_exit_returncode():
Copy link
Member

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

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(
Expand Down