Example #1
0
    def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
        """Fail unless an exception of class excClass is thrown
           by callableObj when invoked with arguments args and keyword
           arguments kwargs. If a different type of exception is
           thrown, it will not be caught, and the test case will be
           deemed to have suffered an error, exactly as for an
           unexpected exception.

           If called with callableObj omitted or None, will return a
           context object used like this::

                with self.assertRaises(SomeException):
                    do_something()

           The context manager keeps a reference to the exception as
           the 'exception' attribute. This allows you to inspect the
           exception after the assertion::

               with self.assertRaises(SomeException) as cm:
                   do_something()
               the_exception = cm.exception
               self.assertEqual(the_exception.error_code, 3)
        """
        context = _AssertRaisesContext(excClass, self)
        if callableObj is None:
            return context
        with context:
            callableObj(*args, **kwargs)
Example #2
0
    def assertRaisesRegexp(self, expected_exception, expected_regexp, callable_obj=None, *args, **kwargs):
        if expected_exception is AssertionError and sys.flags.optimize:
            self.skipTest("AssertionError will never be raised, running in optimized " "mode.")

        context = _AssertRaisesContext(expected_exception, self, expected_regexp)
        if callable_obj is None:
            return context
        with context:
            callable_obj(*args, **kwargs)
Example #3
0
    def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
        if excClass is AssertionError and sys.flags.optimize:
            self.skipTest("AssertionError will never be raised, running in optimized " "mode.")

        context = _AssertRaisesContext(excClass, self)
        if callableObj is None:
            return context
        with context:
            callableObj(*args, **kwargs)
Example #4
0
    def expectRaises(self, excClass, callableObj=None, *args, **kwargs):
        context = _AssertRaisesContext(excClass, self)
        if callableObj is None:
            return context
        with context:
            callableObj(*args, **kwargs)

        self._result.testsRun += 1
        self._num_expectations += 1
Example #5
0
    def assertRaisesWithCode(self, excClass, code, callableObj=_sentinel, *args, **kwargs):
        context = _AssertRaisesContext(excClass, self)
        if callableObj is _sentinel:
            return context
        with context:
            callableObj(*args, **kwargs)

        try:
            callableObj(*args, **kwargs)
        except excClass as e:
            self.assertEqual(e.code, code)
Example #6
0
    def assertRaisesWithCode(self,
                             excClass,
                             code,
                             callableObj=_sentinel,
                             *args,
                             **kwargs):
        context = _AssertRaisesContext(excClass, self)
        if callableObj is _sentinel:
            return context
        with context:
            callableObj(*args, **kwargs)

        try:
            callableObj(*args, **kwargs)
        except excClass as e:
            self.assertEqual(e.code, code)
Example #7
0
    def assertRaisesRegexp(self, expected_exception, expected_regexp,
                           callable_obj=None, *args, **kwargs):
        """Asserts that the message in a raised exception matches a regexp.

        Args:
            expected_exception: Exception class expected to be raised.
            expected_regexp: Regexp (re pattern object or string) expected
                    to be found in error message.
            callable_obj: Function to be called.
            args: Extra args.
            kwargs: Extra kwargs.
        """
        context = _AssertRaisesContext(expected_exception, self, expected_regexp)
        if callable_obj is None:
            return context
        with context:
            callable_obj(*args, **kwargs)
Example #8
0
 def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
     context = _AssertRaisesContext(excClass, self)
     if callableObj is None:
         return context
     with context:
         callableObj(*args, **kwargs)
Example #9
0
 def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
     context = _AssertRaisesContext(excClass, self)
     if callableObj is None:
         return context
     with context:
         callableObj(*args, **kwargs)