pytest_sessionfinish: preset exitstatus with UsageErrors

Previously it would be 0.  Setting it to the expected outcome
(EXIT_USAGEERROR) here already helps `pytest_sessionfinish` hooks.
This commit is contained in:
Daniel Hahler 2019-04-18 23:18:59 +02:00
parent 1460ad6027
commit 649d23c8a8
2 changed files with 12 additions and 0 deletions

View File

@ -208,6 +208,7 @@ def wrap_session(config, doit):
initstate = 2 initstate = 2
session.exitstatus = doit(config, session) or 0 session.exitstatus = doit(config, session) or 0
except UsageError: except UsageError:
session.exitstatus = EXIT_USAGEERROR
raise raise
except Failed: except Failed:
session.exitstatus = EXIT_TESTSFAILED session.exitstatus = EXIT_TESTSFAILED

View File

@ -428,9 +428,20 @@ class TestGeneralUsage(object):
assert result.ret == 4 # usage error only if item not found assert result.ret == 4 # usage error only if item not found
def test_report_all_failed_collections_initargs(self, testdir): def test_report_all_failed_collections_initargs(self, testdir):
testdir.makeconftest(
"""
from _pytest.main import EXIT_USAGEERROR
def pytest_sessionfinish(exitstatus):
assert exitstatus == EXIT_USAGEERROR
print("pytest_sessionfinish_called")
"""
)
testdir.makepyfile(test_a="def", test_b="def") testdir.makepyfile(test_a="def", test_b="def")
result = testdir.runpytest("test_a.py::a", "test_b.py::b") result = testdir.runpytest("test_a.py::a", "test_b.py::b")
result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"]) result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"])
result.stdout.fnmatch_lines(["pytest_sessionfinish_called"])
assert result.ret == EXIT_USAGEERROR
@pytest.mark.usefixtures("recwarn") @pytest.mark.usefixtures("recwarn")
def test_namespace_import_doesnt_confuse_import_hook(self, testdir): def test_namespace_import_doesnt_confuse_import_hook(self, testdir):