Esempio n. 1
0
 def generate(g=generator, c=cls):
     try:
         for test in g():
             test_func, arg = self.parseGeneratedTest(test)
             if not callable(test_func):
                 test_func = unbound_method(c, getattr(c, test_func))
             if ismethod(test_func):
                 yield MethodTestCase(test_func, arg=arg, descriptor=g)
             elif callable(test_func):
                 # In this case we're forcing the 'MethodTestCase'
                 # to run the inline function as its test call,
                 # but using the generator method as the 'method of
                 # record' (so no need to pass it as the descriptor)
                 yield MethodTestCase(g, test=test_func, arg=arg)
             else:
                 yield Failure(
                     TypeError,
                     "%s is not a callable or method" % test_func)
     except KeyboardInterrupt:
         raise
     except:
         exc = sys.exc_info()
         yield Failure(exc[0],
                       exc[1],
                       exc[2],
                       address=test_address(generator))
Esempio n. 2
0
 def wanted(attr, cls=cls, sel=self.selector):
     item = getattr(cls, attr, None)
     if isfunction(item):
         item = unbound_method(cls, item)
     elif not ismethod(item):
         return False
     return sel.wantMethod(item)
Esempio n. 3
0
 def wanted(attr, cls=cls, sel=self.selector):
     item = getattr(cls, attr, None)
     if isfunction(item):
         item = unbound_method(cls, item)
     elif not ismethod(item):
         return False
     return sel.wantMethod(item)
Esempio n. 4
0
def try_run(obj, names):
    """Given a list of possible method names, try to run them with the
    provided object. Keep going until something works. Used to run
    setup/teardown methods for module, package, and function tests.
    """
    for name in names:
        func = getattr(obj, name, None)
        if func is not None:
            if type(obj) == types.ModuleType:
                # py.test compatibility
                try:
                    args, varargs, varkw, defaults = inspect.getargspec(func)
                    if hasattr(func, '__call__') and ismethod(func.__call__):
                        args.pop(0)
                except TypeError:
                    # Not a function. If it's callable, call it anyway
                    if hasattr(func, '__call__'):
                        func = func.__call__
                    try:
                        args, varargs, varkw, defaults = \
                            inspect.getargspec(func)
                        args.pop(0) # pop the self off
                    except TypeError:
                        raise TypeError("Attribute %s of %r is not a python "
                                        "function. Only functions or callables"
                                        " may be used as fixtures." %
                                        (name, obj))
                if len(args):
                    log.debug("call fixture %s.%s(%s)", obj, name, obj)
                    return func(obj)
            log.debug("call fixture %s.%s", obj, name)
            return func()
Esempio n. 5
0
    def _makeTest(self, obj, parent=None):
        """Given a test object and its parent, return a test case
        or test suite.
        """
        plug_tests = []
        try:
            addr = test_address(obj)
        except KeyboardInterrupt:
            raise
        except:
            addr = None
        for test in self.config.plugins.makeTest(obj, parent):
            plug_tests.append(test)
        # TODO: is this try/except needed?
        try:
            if plug_tests:
                return self.suiteClass(plug_tests)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            exc = sys.exc_info()
            return Failure(exc[0], exc[1], exc[2], address=addr)

        if isfunction(obj) and parent and not isinstance(
                parent, types.ModuleType):
            # This is a Python 3.x 'unbound method'.  Wrap it with its
            # associated class..
            obj = unbound_method(parent, obj)

        if isinstance(obj, unittest.TestCase):
            return obj
        elif isclass(obj):
            if parent and obj.__module__ != parent.__name__:
                obj = transplant_class(obj, parent.__name__)
            if issubclass(obj, unittest.TestCase):
                return self.loadTestsFromTestCase(obj)
            else:
                return self.loadTestsFromTestClass(obj)
        elif ismethod(obj):
            if parent is None:
                parent = obj.__class__
            if issubclass(parent, unittest.TestCase):
                return parent(obj.__name__)
            else:
                if isgenerator(obj):
                    return self.loadTestsFromGeneratorMethod(obj, parent)
                else:
                    return MethodTestCase(obj)
        elif isfunction(obj):
            if parent and obj.__module__ != parent.__name__:
                obj = transplant_func(obj, parent.__name__)
            if isgenerator(obj):
                return self.loadTestsFromGenerator(obj, parent)
            else:
                return FunctionTestCase(obj)
        else:
            return Failure(TypeError,
                           "Can't make a test from %s" % obj,
                           address=addr)
Esempio n. 6
0
    def _makeTest(self, obj, parent=None):
        """Given a test object and its parent, return a test case
        or test suite.
        """
        plug_tests = []
        try:
            addr = test_address(obj)
        except KeyboardInterrupt:
            raise
        except:
            addr = None
        for test in self.config.plugins.makeTest(obj, parent):
            plug_tests.append(test)
        # TODO: is this try/except needed?
        try:
            if plug_tests:
                return self.suiteClass(plug_tests)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            exc = sys.exc_info()
            return Failure(exc[0], exc[1], exc[2], address=addr)
        
        if isfunction(obj) and parent and not isinstance(parent, types.ModuleType):
	    # This is a Python 3.x 'unbound method'.  Wrap it with its
	    # associated class..
            obj = unbound_method(parent, obj)

        if isinstance(obj, unittest.TestCase):
            return obj
        elif isclass(obj):
            if parent and obj.__module__ != parent.__name__:
                obj = transplant_class(obj, parent.__name__)
            if issubclass(obj, unittest.TestCase):
                return self.loadTestsFromTestCase(obj)
            else:
                return self.loadTestsFromTestClass(obj)
        elif ismethod(obj):
            if parent is None:
                parent = obj.__class__
            if issubclass(parent, unittest.TestCase):
                return parent(obj.__name__)
            else:
                if isgenerator(obj):
                    return self.loadTestsFromGeneratorMethod(obj, parent)
                else:
                    return MethodTestCase(obj)
        elif isfunction(obj):
            isgen = isgenerator(obj)
            if parent and obj.__module__ != parent.__name__:
                obj = transplant_func(obj, parent.__name__)
            if isgen:
                return self.loadTestsFromGenerator(obj, parent)
            else:
                return FunctionTestCase(obj)
        else:
            return Failure(TypeError,
                           "Can't make a test from %s" % obj,
                           address=addr)
Esempio n. 7
0
def _expand_tests(obj, parent):
    if inspect.isfunction(obj) and parent and not isinstance(parent, types.ModuleType):
        # This is a Python 3.x 'unbound method'.  Wrap it with its
        #  associated class..
        obj = unbound_method(parent, obj)

    tests = []
    if isinstance(obj, list):
        tests = obj
    elif ismethod(obj):
        tests = [obj.__name__]

    _tests = []
    for test in tests:
        _tests += _make_dataprovided_tests(parent, test)

    return _tests
Esempio n. 8
0
    def _expand_tests(self, obj, parent):
        if inspect.isfunction(obj) and parent and not isinstance(parent, types.ModuleType):
            # This is a Python 3.x 'unbound method'.  Wrap it with its
            #  associated class..
            obj = unbound_method(parent, obj)

        tests = []
        if isinstance(obj, list):
            tests = obj
        elif ismethod(obj):
            tests = [obj.__name__]

        _tests = []
        for test in tests:
            _tests += self._make_dataprovided_tests(parent, test)

        return _tests
Esempio n. 9
0
 def generate(g=generator, c=cls):
     try:
         for test in g():
             test_func, arg = self.parseGeneratedTest(test)
             if not callable(test_func):
                 test_func = unbound_method(c, getattr(c, test_func))
             if ismethod(test_func):
                 yield MethodTestCase(test_func, arg=arg, descriptor=g)
             elif isfunction(test_func):
                 # In this case we're forcing the 'MethodTestCase'
                 # to run the inline function as its test call,
                 # but using the generator method as the 'method of
                 # record' (so no need to pass it as the descriptor)
                 yield MethodTestCase(g, test=test_func, arg=arg)
             else:
                 yield Failure(TypeError, "%s is not a function or method" % test_func)
     except KeyboardInterrupt:
         raise
     except:
         exc = sys.exc_info()
         yield Failure(exc[0], exc[1], exc[2], address=test_address(generator))