Skip to content

Commit a6e10cc

Browse files
authored
Merge pull request #6181 from blueyed/maxfail-terminal-upstream
terminal: report ``session.shouldfail`` reason (``-x``)
2 parents c232366 + b06f33f commit a6e10cc

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

changelog/6181.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The reason for a stopped session, e.g. with ``--maxfail`` / ``-x`` gets reported.

doc/en/usage.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ To stop the testing process after the first (N) failures:
6666

6767
.. code-block:: bash
6868
69-
pytest -x # stop after first failure
70-
pytest --maxfail=2 # stop after two failures
69+
pytest -x # stop after first failure
70+
pytest --maxfail=2 # stop after two failures
7171
7272
.. _select-tests:
7373

src/_pytest/terminal.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ def _printcollecteditems(self, items):
676676
self._tw.line("{}{}".format(indent + " ", line.strip()))
677677

678678
@pytest.hookimpl(hookwrapper=True)
679-
def pytest_sessionfinish(self, exitstatus):
679+
def pytest_sessionfinish(self, session: Session, exitstatus: ExitCode):
680680
outcome = yield
681681
outcome.get_result()
682682
self._tw.line("")
@@ -691,9 +691,13 @@ def pytest_sessionfinish(self, exitstatus):
691691
self.config.hook.pytest_terminal_summary(
692692
terminalreporter=self, exitstatus=exitstatus, config=self.config
693693
)
694+
if session.shouldfail:
695+
self.write_sep("!", session.shouldfail, red=True)
694696
if exitstatus == ExitCode.INTERRUPTED:
695697
self._report_keyboardinterrupt()
696698
del self._keyboardinterrupt_memo
699+
elif session.shouldstop:
700+
self.write_sep("!", session.shouldstop, red=True)
697701
self.summary_stats()
698702

699703
@pytest.hookimpl(hookwrapper=True)

testing/test_collection.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,11 +852,15 @@ def test_exit_on_collection_with_maxfail_smaller_than_n_errors(testdir):
852852

853853
res = testdir.runpytest("--maxfail=1")
854854
assert res.ret == 1
855-
856855
res.stdout.fnmatch_lines(
857-
["*ERROR collecting test_02_import_error.py*", "*No module named *asdfa*"]
856+
[
857+
"collected 1 item / 1 error",
858+
"*ERROR collecting test_02_import_error.py*",
859+
"*No module named *asdfa*",
860+
"*! stopping after 1 failures !*",
861+
"*= 1 error in *",
862+
]
858863
)
859-
860864
res.stdout.no_fnmatch_line("*test_03*")
861865

862866

@@ -869,14 +873,15 @@ def test_exit_on_collection_with_maxfail_bigger_than_n_errors(testdir):
869873

870874
res = testdir.runpytest("--maxfail=4")
871875
assert res.ret == 2
872-
873876
res.stdout.fnmatch_lines(
874877
[
875878
"collected 2 items / 2 errors",
876879
"*ERROR collecting test_02_import_error.py*",
877880
"*No module named *asdfa*",
878881
"*ERROR collecting test_03_import_error.py*",
879882
"*No module named *asdfa*",
883+
"*! Interrupted: 2 errors during collection !*",
884+
"*= 2 errors in *",
880885
]
881886
)
882887

testing/test_terminal.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,31 @@ def test_3():
963963
)
964964
result = testdir.runpytest("--maxfail=2", *option.args)
965965
result.stdout.fnmatch_lines(
966-
["*def test_1():*", "*def test_2():*", "*2 failed*"]
966+
[
967+
"*def test_1():*",
968+
"*def test_2():*",
969+
"*! stopping after 2 failures !*",
970+
"*2 failed*",
971+
]
972+
)
973+
974+
def test_maxfailures_with_interrupted(self, testdir):
975+
testdir.makepyfile(
976+
"""
977+
def test(request):
978+
request.session.shouldstop = "session_interrupted"
979+
assert 0
980+
"""
981+
)
982+
result = testdir.runpytest("--maxfail=1", "-ra")
983+
result.stdout.fnmatch_lines(
984+
[
985+
"*= short test summary info =*",
986+
"FAILED *",
987+
"*! stopping after 1 failures !*",
988+
"*! session_interrupted !*",
989+
"*= 1 failed in*",
990+
]
967991
)
968992

969993
def test_tb_option(self, testdir, option):

0 commit comments

Comments
 (0)