Skip to content

Commit

Permalink
Adding Failed exception to manage maxfail behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jespino committed Oct 17, 2017
1 parent 3676da5 commit c5550bc
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def wrap_session(config, doit):
session.exitstatus = doit(config, session) or 0
except UsageError:
raise
except Failed:
session.exitstatus = EXIT_TESTSFAILED
except KeyboardInterrupt:
excinfo = _pytest._code.ExceptionInfo()
if initstate < 2 and isinstance(excinfo.value, exit.Exception):
Expand Down Expand Up @@ -169,6 +171,8 @@ def pytest_runtestloop(session):
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
if session.shouldstop:
raise session.Interrupted(session.shouldstop)
if session.shouldfail:
raise session.Failed(session.shouldfail)
return True


Expand Down Expand Up @@ -590,15 +594,21 @@ class Interrupted(KeyboardInterrupt):
__module__ = 'builtins' # for py3


class Failed(Exception):
""" signals an stop as failed test run. """


class Session(FSCollector):
Interrupted = Interrupted
Failed = Failed

def __init__(self, config):
FSCollector.__init__(self, config.rootdir, parent=None,
config=config, session=self)
self.testsfailed = 0
self.testscollected = 0
self.shouldstop = False
self.shouldfail = False
self.trace = config.trace.root.get("collection")
self._norecursepatterns = config.getini("norecursedirs")
self.startdir = py.path.local()
Expand All @@ -618,7 +628,7 @@ def pytest_runtest_logreport(self, report):
self.testsfailed += 1
maxfail = self.config.getvalue("maxfail")
if maxfail and self.testsfailed >= maxfail:
self.shouldstop = "stopping after %d failures" % (
self.shouldfail = "stopping after %d failures" % (
self.testsfailed)
pytest_collectreport = pytest_runtest_logreport

Expand Down

0 comments on commit c5550bc

Please sign in to comment.