Exemplo n.º 1
0
def assert_raises(exception_class, callable, *args, **kwargs):
    """
    Assert that a callable raises an exception, similar to unittest.py's
    assertRaises.

    Takes more ideas and code from recipe
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307970
    """
    from testoob.utils import _pop
    expected_args = _pop(kwargs, "expected_args", None)
    expected_regex = _pop(kwargs, "expected_regex", None)

    def callsig():
        return _call_signature(callable, *args, **kwargs)

    def exc_name():
        return getattr(exception_class, "__name__", str(exception_class))

    try:
        callable(*args, **kwargs)
    except exception_class, e:
        if expected_args is not None:
            assert_equals(
                expected_args,
                e.args,
                msg="%s raised %s with unexpected args: expected=%r, actual=%r"
                % (callsig(), e.__class__, expected_args, e.args))
        if expected_regex is not None:
            assert_matches(
                expected_regex,
                str(e),
                msg=
                "%s raised %s, but the regular expression '%s' doesn't match %r"
                % (callsig(), e.__class__, expected_regex, str(e)))
Exemplo n.º 2
0
    def failUnlessRaises(self, message, exceptionClass, callableObj, *args,
                         **kwargs):
        """
      A replacement failUnlessRaises.
      unittest.failUnlessRaises() doesn't work with Vim objects.
      ex: self.failUnlessRaises(Vim.Fault.InvalidPowerState, vm.Suspend, tmsg)
      will try for attributes: __name__ and if not set uses str(fault) instead
      of returning the name of the fault this test was expecting.
      """
        from testoob.utils import _pop
        from testoob.testing import TestoobAssertionError, _call_signature
        expectedArgs = _pop(kwargs, "expectedArgs", None)
        expectedRegex = _pop(kwargs, "expectedRegex", None)

        def callsig():
            """Return call signature."""
            return _call_signature(callableObj, *args, **kwargs)

        def exc_name():
            """Return exception class name."""
            if hasattr(exceptionClass, '__name__'):
                exception_name = exceptionClass.__name__
            elif hasattr(exceptionClass, '_typeName'):
                exception_name = exceptionClass._typeName
            else:
                exception_name = str(exceptionClass)
            return exception_name

        try:
            callableObj(*args, **kwargs)
        except exceptionClass as e:
            if expectedArgs is not None:
                testoob.assert_equals(
                      expectedArgs, e.args,
                      msg="%s; %s raised %s with unexpected args: " \
                          "expected=%r, actual=%r" % \
                          (message, callsig(), e.__class__, expectedArgs, e.args)
                   )
            if expectedRegex is not None:
                testoob.assert_matches(
                      expectedRegex, str(e),
                      msg="%s; %s raised %s, but the regular expression '%s' " \
                          "doesn't match %r" % \
                          (message, callsig(), e.__class__, expectedRegex, str(e))
                   )
        except:
            msg = "%s; %s raised an unexpected exception type: " \
                  "expected=%s, actual=%s" % \
                  (message, callsig(), exc_name(), sys.exc_info()[0])
            raise TestoobAssertionError(msg,
                                        description="failUnlessRaises failed")
        else:
            raise TestoobAssertionError(
                  "%s; %s not raised" % \
                  (message, exc_name()), description="failUnlessRaises failed")
Exemplo n.º 3
0
def text_run(*args, **kwargs):
    """
    Run suites with a TextStreamReporter.
    """
    from testoob.utils import _pop

    kwargs.setdefault("reporters", [])

    import sys
    from testoob.reporting import TextStreamReporter
    reporter_class = _pop(kwargs, "reporter_class", TextStreamReporter)

    from testoob.reporting.options import silent
    if not silent:
        kwargs["reporters"].append(reporter_class(stream=sys.stderr))

    # Always have at least one base reporter, so isSuccessful always works
    if len(kwargs["reporters"]) == 0:
        from testoob.reporting.base import BaseReporter
        kwargs["reporters"].append(BaseReporter())

    from testoob.reporting.options import coverage
    for reporter in kwargs["reporters"]:
        reporter.setCoverageInfo(*coverage)

    return run(*args, **kwargs)
Exemplo n.º 4
0
def text_run(*args, **kwargs):
    """
    Run suites with a TextStreamReporter.
    """
    from testoob.utils import _pop

    kwargs.setdefault("reporters", [])

    import sys
    from testoob.reporting import TextStreamReporter
    reporter_class = _pop(kwargs, "reporter_class", TextStreamReporter)

    from testoob.reporting.options import silent
    if not silent:
        kwargs["reporters"].append(reporter_class(stream=sys.stderr))

    # Always have at least one base reporter, so isSuccessful always works
    if len(kwargs["reporters"]) == 0:
        from testoob.reporting.base import BaseReporter
        kwargs["reporters"].append(BaseReporter())

    from testoob.reporting.options import coverage
    for reporter in kwargs["reporters"]:
        reporter.setCoverageInfo(*coverage)

    return run(*args, **kwargs)
Exemplo n.º 5
0
def assert_raises(exception_class, callable, *args, **kwargs):
    """
    Assert that a callable raises an exception, similar to unittest.py's
    assertRaises.

    Takes more ideas and code from recipe
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307970
    """
    from testoob.utils import _pop

    expected_args = _pop(kwargs, "expected_args", None)
    expected_regex = _pop(kwargs, "expected_regex", None)

    def callsig():
        return _call_signature(callable, *args, **kwargs)

    def exc_name():
        return getattr(exception_class, "__name__", str(exception_class))

    try:
        callable(*args, **kwargs)
    except exception_class, e:
        if expected_args is not None:
            assert_equals(
                expected_args,
                e.args,
                msg="%s raised %s with unexpected args: expected=%r, actual=%r"
                % (callsig(), e.__class__, expected_args, e.args),
            )
        if expected_regex is not None:
            assert_matches(
                expected_regex,
                str(e),
                msg="%s raised %s, but the regular expression '%s' doesn't match %r"
                % (callsig(), e.__class__, expected_regex, str(e)),
            )