From 649d23c8a8cf86895830d256be4f6ee1f0f8d1e4 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 18 Apr 2019 23:18:59 +0200 Subject: [PATCH 1/2] 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. --- src/_pytest/main.py | 1 + testing/acceptance_test.py | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index df4a7a956..64e7110b4 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -208,6 +208,7 @@ def wrap_session(config, doit): initstate = 2 session.exitstatus = doit(config, session) or 0 except UsageError: + session.exitstatus = EXIT_USAGEERROR raise except Failed: session.exitstatus = EXIT_TESTSFAILED diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 13a765411..4fb7bc02b 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -428,9 +428,20 @@ class TestGeneralUsage(object): assert result.ret == 4 # usage error only if item not found 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") 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.stdout.fnmatch_lines(["pytest_sessionfinish_called"]) + assert result.ret == EXIT_USAGEERROR @pytest.mark.usefixtures("recwarn") def test_namespace_import_doesnt_confuse_import_hook(self, testdir): From 4749dca764bf8c82f20e7bb86f44265cfa488dc9 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 18 Apr 2019 23:54:16 +0200 Subject: [PATCH 2/2] changelog [ci skip] --- changelog/5144.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/5144.bugfix.rst diff --git a/changelog/5144.bugfix.rst b/changelog/5144.bugfix.rst new file mode 100644 index 000000000..c8c270288 --- /dev/null +++ b/changelog/5144.bugfix.rst @@ -0,0 +1 @@ +With usage errors ``exitstatus`` is set to ``EXIT_USAGEERROR`` in the ``pytest_sessionfinish`` hook now as expected.