Skip to content

Commit 79d5fc3

Browse files
authored
Merge pull request #5144 from blueyed/sessionfinish-exitstatus
pytest_sessionfinish: preset exitstatus with UsageErrors
2 parents 322d686 + 4749dca commit 79d5fc3

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

changelog/5144.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
With usage errors ``exitstatus`` is set to ``EXIT_USAGEERROR`` in the ``pytest_sessionfinish`` hook now as expected.

src/_pytest/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def wrap_session(config, doit):
208208
initstate = 2
209209
session.exitstatus = doit(config, session) or 0
210210
except UsageError:
211+
session.exitstatus = EXIT_USAGEERROR
211212
raise
212213
except Failed:
213214
session.exitstatus = EXIT_TESTSFAILED

testing/acceptance_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,20 @@ def test_a():
428428
assert result.ret == 4 # usage error only if item not found
429429

430430
def test_report_all_failed_collections_initargs(self, testdir):
431+
testdir.makeconftest(
432+
"""
433+
from _pytest.main import EXIT_USAGEERROR
434+
435+
def pytest_sessionfinish(exitstatus):
436+
assert exitstatus == EXIT_USAGEERROR
437+
print("pytest_sessionfinish_called")
438+
"""
439+
)
431440
testdir.makepyfile(test_a="def", test_b="def")
432441
result = testdir.runpytest("test_a.py::a", "test_b.py::b")
433442
result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"])
443+
result.stdout.fnmatch_lines(["pytest_sessionfinish_called"])
444+
assert result.ret == EXIT_USAGEERROR
434445

435446
@pytest.mark.usefixtures("recwarn")
436447
def test_namespace_import_doesnt_confuse_import_hook(self, testdir):

0 commit comments

Comments
 (0)