Skip to content

Commit 00d3abe

Browse files
committed
Adding Failed exception to manage maxfail behavior
1 parent 71c76d9 commit 00d3abe

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

_pytest/main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def wrap_session(config, doit):
102102
session.exitstatus = doit(config, session) or 0
103103
except UsageError:
104104
raise
105+
except Failed:
106+
session.exitstatus = EXIT_TESTSFAILED
105107
except KeyboardInterrupt:
106108
excinfo = _pytest._code.ExceptionInfo()
107109
if initstate < 2 and isinstance(excinfo.value, exit.Exception):
@@ -159,6 +161,8 @@ def pytest_runtestloop(session):
159161
for i, item in enumerate(session.items):
160162
nextitem = session.items[i + 1] if i + 1 < len(session.items) else None
161163
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
164+
if session.shouldfail:
165+
raise session.Failed(session.shouldfail)
162166
if session.shouldstop:
163167
raise session.Interrupted(session.shouldstop)
164168
return True
@@ -564,15 +568,21 @@ class Interrupted(KeyboardInterrupt):
564568
__module__ = 'builtins' # for py3
565569

566570

571+
class Failed(Exception):
572+
""" signals an stop as failed test run. """
573+
574+
567575
class Session(FSCollector):
568576
Interrupted = Interrupted
577+
Failed = Failed
569578

570579
def __init__(self, config):
571580
FSCollector.__init__(self, config.rootdir, parent=None,
572581
config=config, session=self)
573582
self.testsfailed = 0
574583
self.testscollected = 0
575584
self.shouldstop = False
585+
self.shouldfail = False
576586
self.trace = config.trace.root.get("collection")
577587
self._norecursepatterns = config.getini("norecursedirs")
578588
self.startdir = py.path.local()
@@ -583,6 +593,8 @@ def _makeid(self):
583593

584594
@hookimpl(tryfirst=True)
585595
def pytest_collectstart(self):
596+
if self.shouldfail:
597+
raise self.Failed(self.shouldfail)
586598
if self.shouldstop:
587599
raise self.Interrupted(self.shouldstop)
588600

@@ -592,7 +604,7 @@ def pytest_runtest_logreport(self, report):
592604
self.testsfailed += 1
593605
maxfail = self.config.getvalue("maxfail")
594606
if maxfail and self.testsfailed >= maxfail:
595-
self.shouldstop = "stopping after %d failures" % (
607+
self.shouldfail = "stopping after %d failures" % (
596608
self.testsfailed)
597609
pytest_collectreport = pytest_runtest_logreport
598610

changelog/2845.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Change return value of pytest command when maxfail is reached from 2 to 1.

testing/test_collection.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,11 @@ def test_exit_on_collection_with_maxfail_smaller_than_n_errors(testdir):
767767
testdir.makepyfile(**COLLECTION_ERROR_PY_FILES)
768768

769769
res = testdir.runpytest("--maxfail=1")
770-
assert res.ret == 2
770+
assert res.ret == 1
771771

772772
res.stdout.fnmatch_lines([
773773
"*ERROR collecting test_02_import_error.py*",
774774
"*No module named *asdfa*",
775-
"*Interrupted: stopping after 1 failures*",
776775
])
777776

778777
assert 'test_03' not in res.stdout.str()
@@ -824,10 +823,9 @@ def test_continue_on_collection_errors_maxfail(testdir):
824823
testdir.makepyfile(**COLLECTION_ERROR_PY_FILES)
825824

826825
res = testdir.runpytest("--continue-on-collection-errors", "--maxfail=3")
827-
assert res.ret == 2
826+
assert res.ret == 1
828827

829828
res.stdout.fnmatch_lines([
830829
"collected 2 items / 2 errors",
831-
"*Interrupted: stopping after 3 failures*",
832830
"*1 failed, 2 error*",
833831
])

testing/test_terminal.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,6 @@ def test_3():
757757
result.stdout.fnmatch_lines([
758758
"*def test_1():*",
759759
"*def test_2():*",
760-
"*!! Interrupted: stopping after 2 failures*!!*",
761760
"*2 failed*",
762761
])
763762

0 commit comments

Comments
 (0)