Пример #1
0
class SmokeTestRunner(SimpleRpcLogicBase):
    '''
    Inspect in all simplerpc modules for a smokeTestModule function.
    Then create a test for each class and runs it.
    '''
    def __post_init__(self):
        self.inspector = ModulesAttributesIterator(self.context)

    def __gatherTests(self, package):
        filter_func = lambda attr, module: callable(attr) \
                                          and hasattr(attr, '__name__') \
                                          and attr.__name__ == 'smokeTestModule'
        func_dict = self.inspector.buildDict(package, filter_func, reload_=False)
        new_dict = {}
        for module, funcs in func_dict.items():
            if len(funcs):
                new_dict[module] = funcs[0]
        return new_dict

    def getModulesWithoutTests(self):
        package = self.__getPackage()
        filter_func = lambda attr, module: isinstance(attr, FunctionType)
        func_dict = self.inspector.buildDict(package, filter_func, reload_=False)
        missing = []
        for module, funcs in func_dict.items():
            has_test = filter(lambda x: x.__name__ == 'smokeTestModule', funcs)
            #smokeTest exists, or is not this module
            if (not len(has_test) or not len(funcs)) \
              and not module.__name__.split('.')[-1] == self.__class__.__name__:
                missing.append(module)
        return missing

    def __getPackage(self):
        import simplerpc
        return simplerpc

    def runTests(self):
        func_dict = self.__gatherTests(self.__getPackage())
        self.__runFunctions(func_dict)

    def __createTestClass(self, func):
        log = self.context.log
        class SmokeTest(TestCase):
            def testSmokeTest(self):
                log('Testing %s' % func.__module__)
                func()
        return SmokeTest

    def __runFunctions(self, func_dict):
        suites = []
        for _, func in func_dict.items():
            s = TestLoader().loadTestsFromTestCase(self.__createTestClass(func))
            suites.append(s)
        big_suite = TestSuite(suites)
        TextTestRunner().run(big_suite)
class ExposedPackageBrowser(SimpleRpcLogicBase):
    '''
    #TODO: document
    '''
    def __post_init__(self):
        self.modules_inspector = ModulesAttributesIterator(self.context)

    def __buildDict(self, package, filter_func, reload_=True):
        return self.modules_inspector.buildDict(package, filter_func, reload_)

    def getExposedClasses(self, package):
        filter_func = lambda attr, module: isclass(attr) \
                                            and issubclass(attr, ExposedBase) \
                                            and attr.__module__ == module.__name__
        classes = self.__buildDict(package, filter_func).values()
        classes = [c[0] for c in classes if len(c)]
        return classes

    def getModuleAndClass(self, package):
        filter_func = lambda attr, module: isclass(attr) \
                                            and issubclass(attr, ExposedBase) \
                                            and attr.__module__ == module.__name__
        classes = self.__buildDict(package, filter_func)
        classes = [(m, c[0]) for m, c in classes.items() if len(c)]
        return classes
 def __post_init__(self):
     self.modules_inspector = ModulesAttributesIterator(self.context)