[ACCEPTED]-PyUnit: stop after first failing test?-unit-testing

Accepted answer
Score: 27

Nine years after the question was asked, this 15 is still one of the top search results for 14 "python unit test fail early" and, as 13 I discovered when looking at the other search 12 results, these answers are no longer correct 11 for more recent versions of the unittest 10 module.

The documentation for the unittest 9 module https://docs.python.org/3/library/unittest.html#command-line-options and https://docs.python.org/2.7/library/unittest.html#command-line-options show that there is an argument, failfast=True, that 8 can be added to unittest.main, or equivalently 7 a command line option, -f, or --failfast, to 6 stop the test run on the first error or 5 failure. This option was added in version 4 2.7. Using that option is a lot easier than 3 the previously-necessary workarounds suggested 2 in the other answers.

That is, simply change 1 your

unittest.main()

to

unittest.main(failfast=True)
Score: 7

It's a feature. If you want to override 8 this, you'll need to subclass TestCase and/or TestSuite classes 7 and override logic in the run() method.

P.S.: I 6 think you have to subclass unittest.TestCase and override 5 method run() in your class:

def run(self, result=None):
    if result is None: result = self.defaultTestResult()
    result.startTest(self)
    testMethod = getattr(self, self._testMethodName)
    try:
        try:
            self.setUp()
        except KeyboardInterrupt:
            raise
        except:
            result.addError(self, self._exc_info())
            return

        ok = False
        try:
            testMethod()
            ok = True
        except self.failureException:
            result.addFailure(self, self._exc_info())
            result.stop()
        except KeyboardInterrupt:
            raise
        except:
            result.addError(self, self._exc_info())
            result.stop()

        try:
            self.tearDown()
        except KeyboardInterrupt:
            raise
        except:
            result.addError(self, self._exc_info())
            ok = False
        if ok: result.addSuccess(self)
    finally:
        result.stopTest(self)

(I've added two result.stop() calls 4 to the default run definition).

Then you'll 3 have to modify all your testcases to make 2 them subclasses of this new class, instead 1 of unittest.TestCase.

WARNING: I didn't test this code. :)

Score: 7

Based on Eugene's guidance, I've come up 6 with the following:

class TestCase(unittest.TestCase):  
    def run(self, result=None):
        if result.failures or result.errors:
            print "aborted"
        else:
            super(TestCase, self).run(result)

While this works fairly 5 well, it's a bit annoying that each individual 4 test module has to define whether it wants 3 to use this custom class or the default 2 one (a command-line switch, similar to py.test's 1 --exitfirst, would be ideal)...

Score: 0

Building on AnC's answer, this is what I'm 1 using...

def aborting_run(self, result=None):
    if result.failures or result.errors:
        print "aborted"
    else:
        original_run(self, result)
original_run = unittest.TestCase.run
unittest.TestCase.run = aborting_run

More Related questions