Example #1
0
    def useFixture(self, fixture):
        """Use fixture in a test case.

        The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called.

        :param fixture: The fixture to use.
        :return: The fixture, after setting it up and scheduling a cleanup for
           it.
        """
        try:
            fixture.setUp()
        except:
            exc_info = sys.exc_info()
            try:
                gather_details(fixture.getDetails(), self.getDetails())
            except:
                # Report the setUp exception, then raise the error during
                # gather_details.
                self._report_traceback(exc_info)
                raise
            else:
                # Gather_details worked, so raise the exception setUp
                # encountered.
                reraise(*exc_info)
        else:
            self.addCleanup(fixture.cleanUp)
            self.addCleanup(
                gather_details, fixture.getDetails(), self.getDetails())
            return fixture
Example #2
0
    def __call__(self, raise_errors=True):
        """Run all the registered functions.

        :param raise_errors: Deprecated parameter from before testtools gained
            MultipleExceptions. raise_errors defaults to True. When True
            if exception(s) are raised while running functions, they are
            re-raised after all the functions have run.  If multiple exceptions
            are raised, they are all wrapped into a MultipleExceptions object,
            and that is raised.

            Thus, to catch a specific exception from a function run by __call__,
            you need to catch both the exception and MultipleExceptions, and
            then check within a MultipleExceptions instance for an occurance of
            the type you wish to catch.
        :return: Either None or a list of the exc_info() for each exception
            that occured if raise_errors was False.
        """
        cleanups = reversed(self._cleanups)
        self._cleanups = []
        result = []
        for cleanup, args, kwargs in cleanups:
            try:
                cleanup(*args, **kwargs)
            except Exception:
                result.append(sys.exc_info())
        if result and raise_errors:
            if 1 == len(result):
                error = result[0]
                reraise(error[0], error[1], error[2])
            else:
                raise MultipleExceptions(*result)
        if not raise_errors:
            return result
Example #3
0
    def useFixture(self, fixture):
        """Use fixture in a test case.

        The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called.

        :param fixture: The fixture to use.
        :return: The fixture, after setting it up and scheduling a cleanup for
           it.
        """
        try:
            fixture.setUp()
        except:
            exc_info = sys.exc_info()
            try:
                gather_details(fixture.getDetails(), self.getDetails())
            except:
                # Report the setUp exception, then raise the error during
                # gather_details.
                self._report_traceback(exc_info)
                raise
            else:
                # Gather_details worked, so raise the exception setUp
                # encountered.
                reraise(*exc_info)
        else:
            self.addCleanup(fixture.cleanUp)
            self.addCleanup(gather_details, fixture.getDetails(),
                            self.getDetails())
            return fixture
Example #4
0
    def __call__(self, raise_errors=True):
        """Run all the registered functions.

        :param raise_errors: Deprecated parameter from before testtools gained
            MultipleExceptions. raise_errors defaults to True. When True
            if exception(s) are raised while running functions, they are
            re-raised after all the functions have run.  If multiple exceptions
            are raised, they are all wrapped into a MultipleExceptions object,
            and that is raised.
            Thus, to cach a specific exception from a function run by __call__,
            you need to catch both the exception and MultipleExceptions, and
            then check within a MultipleExceptions instance for an occurance of
            the type you wish to catch.
        :return: Either None or a list of the exc_info() for each exception
            that occured if raise_errors was False.
        """
        cleanups = reversed(self._cleanups)
        self._cleanups = []
        result = []
        for cleanup, args, kwargs in cleanups:
            try:
                cleanup(*args, **kwargs)
            except Exception:
                result.append(sys.exc_info())
        if result and raise_errors:
            if 1 == len(result):
                error = result[0]
                reraise(error[0], error[1], error[2])
            else:
                raise MultipleExceptions(*result)
        if not raise_errors:
            return result
Example #5
0
    def parse(self, argv):
        """Evaluate arguments in argv.

        Used arguments are removed from argv.

        :param argv: The arguments to parse.
        :return: The parsed results as a list.
        """
        count = 0
        result = []
        error = None
        while len(argv) > count and (
            self.maximum_count is None or count < self.maximum_count):
            arg = argv[count]
            count += 1
            try:
                result.append(self._parse_one(arg))
            except ValueError:
                # argument rejected this element
                error = sys.exc_info()
                count -= 1
                break
        if count < self.minimum_count:
            if error is not None:
                reraise(error[0], error[1], error[2])
            raise ValueError('not enough arguments present/matched in %s' % argv)
        del argv[:count]
        return result
Example #6
0
 def test_exc_info(self):
     """After reraise exc_info matches plus some extra traceback"""
     try:
         raise ValueError("Bad value")
     except ValueError:
         _exc_info = sys.exc_info()
     try:
         reraise(*_exc_info)
     except ValueError:
         _new_exc_info = sys.exc_info()
     self.assertIs(_exc_info[0], _new_exc_info[0])
     self.assertIs(_exc_info[1], _new_exc_info[1])
     expected_tb = traceback.extract_tb(_exc_info[2])
     self.assertEqual(expected_tb, traceback.extract_tb(_new_exc_info[2])[-len(expected_tb) :])
Example #7
0
 def test_exc_info(self):
     """After reraise exc_info matches plus some extra traceback"""
     try:
         raise ValueError("Bad value")
     except ValueError:
         _exc_info = sys.exc_info()
     try:
         reraise(*_exc_info)
     except ValueError:
         _new_exc_info = sys.exc_info()
     self.assertIs(_exc_info[0], _new_exc_info[0])
     self.assertIs(_exc_info[1], _new_exc_info[1])
     expected_tb = traceback.extract_tb(_exc_info[2])
     self.assertEqual(
         expected_tb,
         traceback.extract_tb(_new_exc_info[2])[-len(expected_tb):])
Example #8
0
    def useFixture(self, fixture):
        """Use fixture in a test case.

        The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called.

        :param fixture: The fixture to use.
        :return: The fixture, after setting it up and scheduling a cleanup for
           it.
        """
        try:
            fixture.setUp()
        except MultipleExceptions as e:
            if (fixtures is not None and
                    e.args[-1][0] is fixtures.fixture.SetupError):
                gather_details(e.args[-1][1].args[0], self.getDetails())
            raise
        except:
            exc_info = sys.exc_info()
            try:
                # fixture._details is not available if using the newer
                # _setUp() API in Fixtures because it already cleaned up
                # the fixture.  Ideally this whole try/except is not
                # really needed any more, however, we keep this code to
                # remain compatible with the older setUp().
                if (safe_hasattr(fixture, '_details') and
                        fixture._details is not None):
                    gather_details(fixture.getDetails(), self.getDetails())
            except:
                # Report the setUp exception, then raise the error during
                # gather_details.
                self._report_traceback(exc_info)
                raise
            else:
                # Gather_details worked, so raise the exception setUp
                # encountered.
                reraise(*exc_info)
        else:
            self.addCleanup(fixture.cleanUp)
            self.addCleanup(
                gather_details, fixture.getDetails(), self.getDetails())
            return fixture
Example #9
0
    def useFixture(self, fixture):
        """Use fixture in a test case.

        The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called.

        :param fixture: The fixture to use.
        :return: The fixture, after setting it up and scheduling a cleanup for
           it.
        """
        try:
            fixture.setUp()
        except MultipleExceptions as e:
            if (fixtures is not None and
                    e.args[-1][0] is fixtures.fixture.SetupError):
                gather_details(e.args[-1][1].args[0], self.getDetails())
            raise
        except:
            exc_info = sys.exc_info()
            try:
                # fixture._details is not available if using the newer
                # _setUp() API in Fixtures because it already cleaned up
                # the fixture.  Ideally this whole try/except is not
                # really needed any more, however, we keep this code to
                # remain compatible with the older setUp().
                if (safe_hasattr(fixture, '_details') and
                        fixture._details is not None):
                    gather_details(fixture.getDetails(), self.getDetails())
            except:
                # Report the setUp exception, then raise the error during
                # gather_details.
                self._report_traceback(exc_info)
                raise
            else:
                # Gather_details worked, so raise the exception setUp
                # encountered.
                reraise(*exc_info)
        else:
            self.addCleanup(fixture.cleanUp)
            self.addCleanup(
                gather_details, fixture.getDetails(), self.getDetails())
            return fixture
Example #10
0
 def match(self, matchee):
     if not issubclass(matchee[0], excClass):
         reraise(*matchee)
Example #11
0
 def match(self, matchee):
     if not issubclass(matchee[0], exc_class):
         compat.reraise(*matchee)
Example #12
0
 def match(self, matchee):
     if not issubclass(matchee[0], excClass):
         reraise(*matchee)
Example #13
0
 def match(self, matchee):
     if not issubclass(matchee[0], expected_exception):
         compat.reraise(*matchee)
Example #14
0
 def match(self, matchee):
     if not issubclass(matchee[0], exc_class):
         compat.reraise(*matchee)