1import logging
2import unittest
3from traceback import format_tb
4from nose.pyversion import is_base_exception
5
6log = logging.getLogger(__name__)
7
8
9__all__ = ['Failure']
10
11
12class Failure(unittest.TestCase):
13    """Unloadable or unexecutable test.
14
15    A Failure case is placed in a test suite to indicate the presence of a
16    test that could not be loaded or executed. A common example is a test
17    module that fails to import.
18
19    """
20    __test__ = False # do not collect
21    def __init__(self, exc_class, exc_val, tb=None, address=None):
22        log.debug("A failure! %s %s %s", exc_class, exc_val, format_tb(tb))
23        self.exc_class = exc_class
24        self.exc_val = exc_val
25        self.tb = tb
26        self._address = address
27        unittest.TestCase.__init__(self)
28
29    def __str__(self):
30        return "Failure: %s (%s)" % (
31            getattr(self.exc_class, '__name__', self.exc_class), self.exc_val)
32
33    def address(self):
34        return self._address
35
36    def runTest(self):
37        if self.tb is not None:
38            if is_base_exception(self.exc_val):
39                raise self.exc_val, None, self.tb
40            raise self.exc_class, self.exc_val, self.tb
41        else:
42            raise self.exc_class(self.exc_val)
43