This instance will be upgraded to Heptapod 0.21.0rc2 on 2021-04-16 at 11:00 UTC+2 (a few minutes of down time)

nosecaptureexc.py 1.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
"""Nose plugin to capture unhandled exception"""
import sys
from nose import plugins

class CaptureExcPlugin(plugins.Plugin):
    """Capture unhandled exception (probably raised inside event loop)"""
    enabled = False
    name = 'capture-exc'

    def options(self, parser, env):
        parser.add_option('--no-capture-exc', dest='capture_exc',
                          action='store_false', default=True,
                          help='Catch unhandled exception to report as error')

    def configure(self, options, conf):
        self.enabled = options.capture_exc

    def prepareTestResult(self, result):
        # dirty hack to access result.addError()
        self._result = result

    def startTest(self, test):
        self._origexcepthook = sys.excepthook
        sys.excepthook = self._excepthook
        self._excepts = []

    def stopTest(self, test):
        sys.excepthook = self._origexcepthook
        del self._origexcepthook
        if self._excepts:
            # BUG: because the corresponding addSuccess/Failure/Error for the
            # given test is already called, this increases the test counts.
            self._result.addError(test, self._excepts.pop(0))

    def _excepthook(self, type, value, traceback):
        self._excepts.append((type, value, traceback))