def loadTestsFromModule(self, module, direct=True): """Load all tests from module and return a suite containing them. """ tests = [] test_funcs = [] test_classes = [] if self.selector.wantModule(module) or direct: for item in dir(module): test = getattr(module, item, None) if isclass(test): if self.selector.wantClass(test): test_classes.append(test) elif isfunction(test) and self.selector.wantFunction(test): test_funcs.append(test) if PYTHON_VERSION_MAJOR != 3: test_classes.sort(lambda a, b: cmp(a.__name__, b.__name__)) test_funcs.sort(cmp_lineno) tests = map(lambda t: self.makeTest(t, parent=module), test_classes + test_funcs) else: test_classes.sort(key=lambda a: a.__name__) test_funcs.sort(key=func_lineno) tests = [ self.makeTest(t, parent=module) for t in test_classes + test_funcs ] return self.suiteClass(ContextList(tests, context=module))
def loadTestsFromModule(self, module, direct = True): """Load all tests from module and return a suite containing them. """ tests = [] test_funcs = [] test_classes = [] if self.selector.wantModule(module) or direct: for item in dir(module): test = getattr(module, item, None) if isclass(test): if self.selector.wantClass(test): test_classes.append(test) elif isfunction(test) and self.selector.wantFunction(test): test_funcs.append(test) if PYTHON_VERSION_MAJOR != 3: test_classes.sort(lambda a, b: cmp(a.__name__, b.__name__)) test_funcs.sort(cmp_lineno) tests = map(lambda t: self.makeTest(t, parent=module), test_classes + test_funcs) else: test_classes.sort(key = lambda a: a.__name__) test_funcs.sort(key = func_lineno) tests = [self.makeTest(t, parent=module) for t in test_classes + test_funcs] return self.suiteClass(ContextList(tests, context=module))
def teardownContext(self, context): if self.factory: if context in self.factory.was_torndown: return self.factory.was_torndown[context] = self if isclass(context): names = self.classTeardown else: names = self.moduleTeardown if hasattr(context, '__path__'): names = self.packageTeardown + names try_run(context, names)
def setupContext(self, context): if self.factory: if context in self.factory.was_setup: return self.factory.was_setup[context] = self if isclass(context): names = self.classSetup else: names = self.moduleSetup if hasattr(context, '__path__'): names = self.packageSetup + names try_run(context, names)
def _makeTest(self, obj, parent=None): """Given a test object and its parent, return a test case or test suite. """ import inspect try: lineno = inspect.getsourcelines(obj) except: lineno = ("", 1) if isfunction(obj) and parent and not isinstance( parent, types.ModuleType): 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) or isunboundmethod(obj): if parent is None: parent = obj.__class__ if issubclass(parent, unittest.TestCase): return parent(obj.__name__) else: if PYTHON_VERSION_MAJOR > 2: setattr(obj, "im_class", parent) setattr(obj, "im_self", parent) test_case = MethodTestCase(obj) test_case.lineno = lineno[1] return test_case elif isfunction(obj): setattr(obj, "lineno", lineno[1]) if hasattr(obj, "__module__"): if parent and obj.__module__ != parent.__name__: obj = transplant_func(obj, parent.__name__) else: if parent: obj = transplant_func(obj, parent.__name__) else: obj = transplant_func(obj) if isgenerator(obj): return self.loadTestsFromGenerator(obj, parent, lineno[1]) else: return FunctionTestCase(obj) else: return Failure(TypeError, "Can't make a test from %s" % obj)
def _makeTest(self, obj, parent=None): """Given a test object and its parent, return a test case or test suite. """ import inspect try: lineno = inspect.getsourcelines(obj) except: lineno = ("", 1) if isfunction(obj) and parent and not isinstance(parent, types.ModuleType): 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) or isunboundmethod(obj): if parent is None: parent = obj.__class__ if issubclass(parent, unittest.TestCase): return parent(obj.__name__) else: if PYTHON_VERSION_MAJOR > 2: setattr(obj, "im_class", parent) setattr(obj, "im_self", parent) test_case = MethodTestCase(obj) test_case.lineno = lineno[1] return test_case elif isfunction(obj): setattr(obj, "lineno", lineno[1]) if hasattr(obj, "__module__"): if parent and obj.__module__ != parent.__name__: obj = transplant_func(obj, parent.__name__) else: if parent: obj = transplant_func(obj, parent.__name__) else: obj = transplant_func(obj) if isgenerator(obj): return self.loadTestsFromGenerator(obj, parent, lineno[1]) else: return FunctionTestCase(obj) else: return Failure(TypeError, "Can't make a test from %s" % obj)