def _createTests(self, obj): if not hasattr(obj, 'setUp'): if hasattr(obj, 'setup'): obj.setUp = obj.setup elif hasattr(obj, 'setUpFunc'): obj.setUp = obj.setUpFunc if not hasattr(obj, 'tearDown'): if hasattr(obj, 'teardown'): obj.tearDown = obj.teardowns elif hasattr(obj, 'tearDownFunc'): obj.tearDown = obj.tearDownFunc tests = [] args = {} setUp = getattr(obj, 'setUp', None) tearDown = getattr(obj, 'tearDown', None) if setUp is not None: args['setUp'] = setUp if tearDown is not None: args['tearDown'] = tearDown paramList = getattr(obj, 'paramList', None) isGenerator = util.isgenerator(obj) if paramList is not None or isGenerator: return tests else: case = unittest.FunctionTestCase(obj, **args) tests.append(case) return tests
def _createTests(self, obj): if not hasattr(obj, "setUp"): if hasattr(obj, "setup"): obj.setUp = obj.setup elif hasattr(obj, "setUpFunc"): obj.setUp = obj.setUpFunc if not hasattr(obj, "tearDown"): if hasattr(obj, "teardown"): obj.tearDown = obj.teardown elif hasattr(obj, "tearDownFunc"): obj.tearDown = obj.tearDownFunc tests = [] args = {} setUp = getattr(obj, "setUp", None) tearDown = getattr(obj, "tearDown", None) if setUp is not None: args["setUp"] = setUp if tearDown is not None: args["tearDown"] = tearDown paramList = getattr(obj, "paramList", None) isGenerator = util.isgenerator(obj) if paramList is not None or isGenerator: return tests else: case = util.transplant_class(unittest.FunctionTestCase, obj.__module__)(obj, **args) tests.append(case) return tests
def loadTestsFromName(self, event): """Load tests from event.name if it names a test class/method""" name = event.name module = event.module try: result = util.test_from_name(name, module) except (AttributeError, ImportError): event.handled = True return event.loader.failedLoadTests(name, sys.exc_info()) if result is None: return parent, obj, name, index = result if isinstance(obj, type) and not issubclass(obj, unittest.TestCase): # name is a test case class event.extraTests.append(self._loadTestsFromTestClass(event, obj)) elif ( isinstance(parent, type) and not issubclass(parent, unittest.TestCase) and not util.isgenerator(obj) and not hasattr(obj, "paramList") ): # name is a single test method event.extraTests.append( util.transplant_class(MethodTestCase(parent), parent.__module__)( obj.__name__ ) )
def _createTests(self, obj): if not hasattr(obj, 'setUp'): if hasattr(obj, 'setup'): obj.setUp = obj.setup elif hasattr(obj, 'setUpFunc'): obj.setUp = obj.setUpFunc if not hasattr(obj, 'tearDown'): if hasattr(obj, 'teardown'): obj.tearDown = obj.teardown elif hasattr(obj, 'tearDownFunc'): obj.tearDown = obj.tearDownFunc tests = [] args = {} setUp = getattr(obj, 'setUp', None) tearDown = getattr(obj, 'tearDown', None) if setUp is not None: args['setUp'] = setUp if tearDown is not None: args['tearDown'] = tearDown paramList = getattr(obj, 'paramList', None) isGenerator = util.isgenerator(obj) if paramList is not None or isGenerator: return tests else: case = util.transplant_class(FunctionTestCase, obj.__module__)(obj, **args) tests.append(case)
def getTestCaseNames(self, event): """Get generator test case names from test case class""" log.debug('getTestCaseNames %s', event.testCase) names = filter(event.isTestMethod, dir(event.testCase)) klass = event.testCase for name in names: method = getattr(klass, name) if util.isgenerator(method): event.excludedNames.append(name)
def loadTestsFromTestClass(self, event): testCaseClass = event.testCase for name in dir(testCaseClass): method = getattr(testCaseClass, name) if ( name.startswith(self.session.testMethodPrefix) and hasattr(getattr(testCaseClass, name), "__call__") and util.isgenerator(method) ): instance = testCaseClass() event.extraTests.extend(self._testsFromGeneratorMethod(event, name, method, instance))
def loadTestsFromName(self, event): """Load tests from generator named on command line""" original_name = name = event.name module = event.module try: result = util.test_from_name(name, module) except (AttributeError, ImportError): event.handled = True return event.loader.failedLoadTests(name, sys.exc_info()) if result is None: # we can't find it - let the default case handle it return parent, obj, name, index = result if not util.isgenerator(obj): return if (index is None and not isinstance(parent, type) and not isinstance(obj, types.FunctionType)): log.debug("Don't know how to load generator tests from %s", obj) return if (parent and isinstance(parent, type) and issubclass(parent, unittest.TestCase)): # generator method in test case instance = parent(obj.__name__) tests = list( self._testsFromGenerator( event, obj.__name__, obj(instance), parent) ) elif (parent and isinstance(parent, type)): # generator method in test class method = obj instance = parent() tests = list( self._testsFromGeneratorMethod(event, name, method, instance) ) else: # generator func tests = list(self._testsFromGeneratorFunc(event, obj)) if index is not None: try: tests = [tests[index - 1]] except IndexError: raise exceptions.TestNotFoundError(original_name) suite = event.loader.suiteClass() suite.addTests(tests) event.handled = True return suite
def loadTestsFromName(self, event): """Load tests from generator named on command line""" original_name = name = event.name module = event.module try: result = util.test_from_name(name, module) except (AttributeError, ImportError) as e: event.handled = True return event.loader.failedLoadTests(name, e) if result is None: # we can't find it - let the default case handle it return parent, obj, name, index = result if not util.isgenerator(obj): return if (index is None and not isinstance(parent, type) and not isinstance(obj, types.FunctionType)): log.debug("Don't know how to load generator tests from %s", obj) return if (parent and isinstance(parent, type) and issubclass(parent, unittest.TestCase)): # generator method in test case instance = parent(obj.__name__) tests = list( self._testsFromGenerator( event, obj.__name__, obj(instance), parent) ) elif (parent and isinstance(parent, type)): # generator method in test class method = obj instance = parent() tests = list( self._testsFromGeneratorMethod(event, name, method, instance) ) else: # generator func tests = list(self._testsFromGeneratorFunc(event, obj)) if index is not None: try: tests = [tests[index - 1]] except IndexError: raise exceptions.TestNotFoundError(original_name) suite = event.loader.suiteClass() suite.addTests(tests) event.handled = True return suite
def loadTestsFromTestClass(self, event): testCaseClass = event.testCase for name in dir(testCaseClass): method = getattr(testCaseClass, name) if (name.startswith(self.session.testMethodPrefix) and hasattr(getattr(testCaseClass, name), '__call__') and util.isgenerator(method)): instance = testCaseClass() event.extraTests.extend( self._testsFromGeneratorMethod(event, name, method, instance))
def loadTestsFromTestCase(self, event): """Load generator tests from test case""" log.debug('loadTestsFromTestCase %s', event.testCase) testCaseClass = event.testCase for name in dir(testCaseClass): method = getattr(testCaseClass, name) if (name.startswith(self.session.testMethodPrefix) and hasattr(getattr(testCaseClass, name), '__call__') and util.isgenerator(method)): instance = testCaseClass(name) event.extraTests.extend( self._testsFromGenerator(event, name, method(instance), testCaseClass))
def loadTestsFromTestCase(self, event): """Load generator tests from test case""" log.debug("loadTestsFromTestCase %s", event.testCase) testCaseClass = event.testCase for name in dir(testCaseClass): method = getattr(testCaseClass, name) if ( name.startswith(self.session.testMethodPrefix) and hasattr(getattr(testCaseClass, name), "__call__") and util.isgenerator(method) ): instance = testCaseClass(name) event.extraTests.extend(self._testsFromGenerator(event, name, method(instance), testCaseClass))
def loadTestsFromName(self, event): """Load test if event.name is the name of a test function""" name = event.name module = event.module try: result = util.test_from_name(name, module) except (AttributeError, ImportError): event.handled = True return event.loader.failedLoadTests(name, sys.exc_info()) if result is None: return parent, obj, name, index = result if (isinstance(obj, types.FunctionType) and not util.isgenerator(obj) and not hasattr(obj, 'paramList') and util.num_expected_args(obj) == 0): suite = event.loader.suiteClass() suite.addTests(self._createTests(obj)) event.handled = True return suite
def loadTestsFromName(self, event): """Load test if event.name is the name of a test function""" name = event.name module = event.module try: result = util.test_from_name(name, module) except (AttributeError, ImportError) as e: event.handled = True return event.loader.failedLoadTests(name, e) if result is None: return parent, obj, name, index = result if (isinstance(obj, types.FunctionType) and not util.isgenerator(obj) and not hasattr(obj, 'paramList') and not inspect.getargspec(obj).args): suite = event.loader.suiteClass() suite.addTests(self._createTests(obj)) event.handled = True return suite
def loadTestsFromName(self, event): """Load tests from event.name if it names a test case/method""" name = event.name module = event.module try: result = util.test_from_name(name, module) except (AttributeError, ImportError) as e: event.handled = True return event.loader.failedLoadTests(name, e) if result is None: return parent, obj, name, index = result if isinstance(obj, type) and issubclass(obj, unittest.TestCase): # name is a test case class event.extraTests.append(self._loadTestsFromTestCase(event, obj)) elif (isinstance(parent, type) and issubclass(parent, unittest.TestCase) and not util.isgenerator(obj) and not hasattr(obj, 'paramList')): # name is a single test method event.extraTests.append(parent(obj.__name__))
def loadTestsFromName(self, event): """Load tests from event.name if it names a test case/method""" name = event.name module = event.module log.debug("load %s from %s", name, module) try: result = util.test_from_name(name, module) except (AttributeError, ImportError) as e: event.handled = True return event.loader.failedLoadTests(name, e) if result is None: return parent, obj, name, index = result if isinstance(obj, type) and issubclass(obj, unittest.TestCase): # name is a test case class event.extraTests.append(self._loadTestsFromTestCase(event, obj)) elif (isinstance(parent, type) and issubclass(parent, unittest.TestCase) and not util.isgenerator(obj) and not hasattr(obj, 'paramList')): # name is a single test method event.extraTests.append(parent(obj.__name__))
def is_test(obj): return (obj.__name__.startswith(self.session.testMethodPrefix) and util.isgenerator(obj))