示例#1
0
    def assert_try_call(self, timeout, interval, callable_object, *args,
                        **kw_args):
        """
        Passes if callable_object(*args, **kw_args) doesn't fail any
        assertions in <timeout> seconds.
        (It will tried <timeout / interval> times.)

          def random_number():
              number = random.randint(0, 9)
              self.assert_in_delta(5, number, 1)
              return number
          self.assert_try_call(1, 0.1, random_number) # => will pass
                                                      # returns 4, 5 or 6
          self.assert_try_call(1, 0.1, self.fail, "Never succeed") # => fail
        """
        rest = timeout
        while True:
            before = time.time()
            try:
                result = callable_object(*args, **kw_args)
                break
            except pikzie.core.AssertionFailure:
                if rest <= 0:
                    message = \
                        "expected: %s succeeds\n" \
                        " timeout: <%s> seconds\n" \
                        "interval: <%s> seconds\n" \
                        " but was:\n%s" % \
                        (pp.format_call(callable_object, args, kw_args),
                         timeout, interval,
                         str(sys.exc_info()[1]))
                    self._fail(message)
                runtime = time.time() - before
                wait_time = interval - runtime
                if wait_time > 0:
                    time.sleep(wait_time)
                rest -= max(runtime, interval)
        self._pass_assertion()
        return result
示例#2
0
    def assert_try_call(self, timeout, interval,
                        callable_object, *args, **kw_args):
        """
        Passes if callable_object(*args, **kw_args) doesn't fail any
        assertions in <timeout> seconds.
        (It will tried <timeout / interval> times.)

          def random_number():
              number = random.randint(0, 9)
              self.assert_in_delta(5, number, 1)
              return number
          self.assert_try_call(1, 0.1, random_number) # => will pass
                                                      # returns 4, 5 or 6
          self.assert_try_call(1, 0.1, self.fail, "Never succeed") # => fail
        """
        rest = timeout
        while True:
            before = time.time()
            try:
                result = callable_object(*args, **kw_args)
                break
            except pikzie.core.AssertionFailure:
                if rest <= 0:
                    message = \
                        "expected: %s succeeds\n" \
                        " timeout: <%s> seconds\n" \
                        "interval: <%s> seconds\n" \
                        " but was:\n%s" % \
                        (pp.format_call(callable_object, args, kw_args),
                         timeout, interval,
                         str(sys.exc_info()[1]))
                    self._fail(message)
                runtime = time.time() - before
                wait_time = interval - runtime
                if wait_time > 0:
                    time.sleep(wait_time)
                rest -= max(runtime, interval)
        self._pass_assertion()
        return result
示例#3
0
    def assert_nothing_raised_call(self, callable_object, *args, **kw_args):
        """
        Passes if callable_object(*args, **kw_args) raises nothing exception
        and returns called result.

          self.assert_nothing_raised_call(lambda: 1)                # => pass
                                                                    # => returns 1
          self.assert_nothing_raised_call(lambda: unknown_variable) # => fail
        """
        self.assert_callable(callable_object)
        try:
            result = callable_object(*args, **kw_args)
        except:
            actual = sys.exc_info()[:2]
            actual_exception_class, actual_exception_value = actual
            message = \
                "expected: %s nothing raised\n" \
                " but was: <%s>(%s) is raised" % \
                (pp.format_call(callable_object, args, kw_args),
                 pp.format_exception_class(actual_exception_class),
                 str(actual_exception_value))
            self._fail(message)
        self._pass_assertion()
        return result
示例#4
0
    def assert_nothing_raised_call(self, callable_object, *args, **kw_args):
        """
        Passes if callable_object(*args, **kw_args) raises nothing exception
        and returns called result.

          self.assert_nothing_raised_call(lambda: 1)                # => pass
                                                                    # => returns 1
          self.assert_nothing_raised_call(lambda: unknown_variable) # => fail
        """
        self.assert_callable(callable_object)
        try:
            result = callable_object(*args, **kw_args)
        except:
            actual = sys.exc_info()[:2]
            actual_exception_class, actual_exception_value = actual
            message = \
                "expected: %s nothing raised\n" \
                " but was: <%s>(%s) is raised" % \
                (pp.format_call(callable_object, args, kw_args),
                 pp.format_exception_class(actual_exception_class),
                 str(actual_exception_value))
            self._fail(message)
        self._pass_assertion()
        return result
示例#5
0
    def assert_raise_call(self, exception, callable_object, *args, **kw_args):
        """
        Passes if callable_object(*args, **kw_args) raises exception and
        returns raised exception value.

          self.assert_raise_call(NameError,
                                 lambda: unknown_variable) # => pass
                                                           # => returns NameError
                                                           #    value
          self.assert_raise_call(NameError, lambda: 1)     # => fail

        Exception instance can be also passed if it's comparable.

          class ComparableError(Exception):
              def __init__(self, message):
                  self.message = message

              def __repr__(self):
                  return "%s(%r,)" % (type(self).__name__, self.message)

              def __eq__(self, other):
                  return isinstance(other, self.__class__) and \
                          self.message == other.message

          def raise_error():
              raise ComparableError("value")
          self.assert_raise_call(ComparableError("value"),
                                 raise_error)              # => pass
                                                           # => returns
                                                           #    ComparableError
                                                           #    value
          self.assert_raise_call(ComparableError("key"),
                                 raise_error)              # => fail
        """
        self.assert_callable(callable_object)
        if isinstance(exception, Exception):
            exception_class = exception.__class__
        else:
            exception_class = exception
        try:
            callable_object(*args, **kw_args)
        except exception_class:
            actual = sys.exc_info()[1]
            if exception_class == exception:
                self._pass_assertion()
            else:
                self.assert_equal(exception, actual)
            return actual
        except:
            actual = sys.exc_info()
            actual_exception_class, actual_exception_value = actual[:2]
            if exception_class == exception:
                expected = "<%s>" % pp.format_exception_class(exception_class)
            else:
                expected = "<%s>(%s)" % \
                    (pp.format_exception_class(exception_class),
                     str(exception))
            actual = "<%s>(%s)" % \
                (pp.format_exception_class(actual_exception_class),
                 str(actual_exception_value))
            message = \
                "expected: %s is raised\n" \
                " but was: %s" % (expected, actual)
            self._fail(message)
        else:
            message = \
                "expected: <%s> is raised\n" \
                " but was: %s nothing raised" % \
                (pp.format_exception_class(exception_class),
                 pp.format_call(callable_object, args, kw_args))
            self._fail(message)
示例#6
0
    def assert_raise_call(self, exception, callable_object, *args, **kw_args):
        """
        Passes if callable_object(*args, **kw_args) raises exception and
        returns raised exception value.

          self.assert_raise_call(NameError,
                                 lambda: unknown_variable) # => pass
                                                           # => returns NameError
                                                           #    value
          self.assert_raise_call(NameError, lambda: 1)     # => fail

        Exception instance can be also passed if it's comparable.

          class ComparableError(Exception):
              def __init__(self, message):
                  self.message = message

              def __repr__(self):
                  return "%s(%r,)" % (type(self).__name__, self.message)

              def __eq__(self, other):
                  return isinstance(other, self.__class__) and \
                          self.message == other.message

          def raise_error():
              raise ComparableError("value")
          self.assert_raise_call(ComparableError("value"),
                                 raise_error)              # => pass
                                                           # => returns
                                                           #    ComparableError
                                                           #    value
          self.assert_raise_call(ComparableError("key"),
                                 raise_error)              # => fail
        """
        self.assert_callable(callable_object)
        if isinstance(exception, Exception):
            exception_class = exception.__class__
        else:
            exception_class = exception
        try:
            callable_object(*args, **kw_args)
        except exception_class:
            actual = sys.exc_info()[1]
            if exception_class == exception:
                self._pass_assertion()
            else:
                self.assert_equal(exception, actual)
            return actual
        except:
            actual = sys.exc_info()
            actual_exception_class, actual_exception_value = actual[:2]
            if exception_class == exception:
                expected = "<%s>" % pp.format_exception_class(exception_class)
            else:
                expected = "<%s>(%s)" % \
                    (pp.format_exception_class(exception_class),
                     str(exception))
            actual = "<%s>(%s)" % \
                (pp.format_exception_class(actual_exception_class),
                 str(actual_exception_value))
            message = \
                "expected: %s is raised\n" \
                " but was: %s" % (expected, actual)
            self._fail(message)
        else:
            message = \
                "expected: <%s> is raised\n" \
                " but was: %s nothing raised" % \
                (pp.format_exception_class(exception_class),
                 pp.format_call(callable_object, args, kw_args))
            self._fail(message)