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))
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)
def loadTestsFromFile(self, filename): """Load tests from a non-module file. Default is to raise a ValueError; plugins may implement `loadTestsFromFile` to provide a list of tests loaded from the file. """ log.debug("Load from non-module file %s", filename) try: tests = [ test for test in self.config.plugins.loadTestsFromFile(filename) ] if tests: # Plugins can yield False to indicate that they were # unable to load tests from a file, but it was not an # error -- the file just had no tests to load. tests = [_f for _f in tests if _f] return self.suiteClass(tests) else: # Nothing was able to even try to load from this file open(filename, 'r').close() # trigger os error raise ValueError("Unable to load tests from file %s" % filename) except (KeyboardInterrupt, SystemExit): raise except: exc = sys.exc_info() return self.suiteClass([ Failure(exc[0], exc[1], exc[2], address=(filename, None, None)) ])
def makeTest(self, obj, parent=None): """Given a test object and its parent, return a test case or test suite. """ plug_tests = [] 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: return Failure(*sys.exc_info()) 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)
def resolve(self, name, module): """Resolve name within module """ obj = module parts = name.split('.') for part in parts: parent, obj = obj, getattr(obj, part, None) if obj is None: # no such test obj = Failure(ValueError, "No such test %s" % name) return parent, obj
def generate(g=generator, c=testCaseClass): try: for test in g(): test_func, arg = (test[0], test[1:]) yield QUnitMethodTestCase(test_func, arg=arg, descriptor=g) except KeyboardInterrupt: raise except: exc = sys.exc_info() yield Failure(exc[0], exc[1], exc[2], address=test_address(generator))
def test_failure_to_parse(self): failed_test = Test( Failure( Exception, 'msg', address=TestAddress( 'k2.finder.test.test_test_finder:TestOfFinderMappingNoseTestsToK2TestCases.test_function' ).totuple())) suite = [failed_test] actual = Finder(self.component_manager)._test_cases_from_suite(suite) with self.assertRaises(Exception, msg='msg'): actual[0].run_function()
def generate(g=generator, m=module): try: for test in g(): test_func, arg = self.parseGeneratedTest(test) if not isinstance(test_func, collections.Callable): test_func = getattr(m, test_func) yield FunctionTestCase(test_func, arg=arg, descriptor=g) except KeyboardInterrupt: raise except: exc = sys.exc_info() yield Failure(exc[0], exc[1], exc[2], address=test_address(generator))
def makeTest(self, obj, parent=None): try: return self._makeTest(obj, parent) except (KeyboardInterrupt, SystemExit): raise except: exc = sys.exc_info() try: addr = test_address(obj) except KeyboardInterrupt: raise except: addr = None return Failure(exc[0], exc[1], exc[2], address=addr)
def makeTest(self, obj, parent=None): """Given a test object and its parent, return a test case or test suite. """ ldr = loader.TestLoader() if isinstance(obj, unittest.TestCase): return obj elif isclass(obj): if not self.class_specific: if parent and obj.__module__ != parent.__name__: obj = transplant_class(obj, parent.__name__) if issubclass(obj, unittest.TestCase): # Randomize the order of the tests in the TestCase return self.randomized_loadTestsFromTestCase(obj) else: return self.randomized_loadTestsFromTestClass(obj) else: class_specific_seed = getattr( obj, CLASS_SPECIFIC_RANDOMIZE_TESTS_FIELD_NAME, None) if issubclass( obj, unittest.TestCase) and class_specific_seed is not None: random.seed(class_specific_seed) print("Using %d as seed to randomize tests in %s" % (class_specific_seed, '.'.join( [obj.__module__, obj.__name__]))) return self.randomized_loadTestsFromTestCase(obj) else: return ldr.loadTestsFromTestCase(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 ldr.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 ldr.loadTestsFromGenerator(obj, parent) else: return [FunctionTestCase(obj)] else: return Failure(TypeError, "Can't make a test from %s" % obj)
def loadTestsFromFile(self, filename, indexes=[]): log.debug("Loading from file %s" % filename) step_registry = StepImplRegistry(TagMatcher) try: feat = load_feature(filename, self.language) path = os.path.dirname(filename) self.impl_loader.load_steps_impl(step_registry, path, feat.use_step_defs) except ParseException, e: ec, ev, tb = sys.exc_info() yield Failure( ParseException, ParseException(e.pstr, e.loc, e.msg + " in %s" % filename), tb) return
def generate(self, *arg, **kw): """Call all plugins, yielding each item in each non-None result. """ for p, meth in self.plugins: result = None try: result = meth(*arg, **kw) if result is not None: for r in result: yield r except (KeyboardInterrupt, SystemExit): raise except: exc = sys.exc_info() yield Failure(*exc) continue
def generate(g=generator, c=cls): for test in g(): try: test_func, arg = (test[0], test[1:]) except ValueError: test_func, arg = test[0], tuple() if not callable(test_func): test_func = 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)
def loadTestsFromFile(self, filename): """ We'll exploit the knowledge of the selector implementation details: since the selector 'wants' only root build.xml file for JUnit-based tests, we can safely check initials and then fall back to inherited behavior or collect output for all tests. """ log.debug("JUnitTestLoader.loadTestsFromFile(%s)", filename) if not filename.startswith(self.compiledTestsRoot): log.debug( "JUnitTestLoader.loadTestsFromFile(): fall back to predefined behavior" ) log.debug( "JUnitTestLoader.loadTestsFromFile(): compiledTestsRoot is %s", self.compiledTestsRoot) return TestLoader.loadTestsFromFile(self, filename) commandLine = self.prepareCommandLine() try: testOutput = self.executeTests(commandLine) except Exception as e: log.debug("Building JUnit test suite failed with message: %s", str(e)) return self.suiteClass( Failure(Exception, "Couldn't build compiled test suite.")) log.debug("JUnitTestLoader.loadTestsFromFile(): got ant output:\n%s", testOutput) singleTestOutput, singleTestExceptions = self.filterTestOutput( testOutput) log.debug( "JUnitTestLoader.loadTestsFromFile(): JUnit filtered output:") for line in singleTestOutput: log.debug("%s\n", line) log.debug("JUnitTestLoader.loadTestsFromFile(): %s", "JUnit filtered output finished.") loadedTests = self.makeTestCases(singleTestOutput, singleTestExceptions) log.debug("JUnitTestLoader.loadTestsFromFile(): loaded tests %s", loadedTests) log.debug("JUnitTestLoader.loadTestsFromFile(): suite class is %s", repr(self.suiteClass)) return self.suiteClass(loadedTests)
def makeTest(self, obj, parent=None): """Given a test object and its parent, return a test case or test suite. """ ldr = loader.TestLoader() 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): # Randomize the order of the tests in the TestCase return self.Randomized_loadTestsFromTestCase(obj) else: return ldr.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 ldr.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 ldr.loadTestsFromGenerator(obj, parent) else: return FunctionTestCase(obj) else: return Failure(TypeError, "Can't make a test from %s" % obj)
def loadTestsFromName(self, name, module=None, discovered=False): """Load tests from the entity with the given name. The name may indicate a file, directory, module, or any object within a module. See `nose.util.split_test_name` for details on test name parsing. """ # FIXME refactor this method into little bites? log.debug("load from %s (%s)", name, module) suite = self.suiteClass # give plugins first crack plug_tests = self.config.plugins.loadTestsFromName(name, module) if plug_tests: return suite(plug_tests) addr = TestAddress(name, workingDir=self.workingDir) if module: # Two cases: # name is class.foo # The addr will be incorrect, since it thinks class.foo is # a dotted module name. It's actually a dotted attribute # name. In this case we want to use the full submitted # name as the name to load from the module. # name is module:class.foo # The addr will be correct. The part we want is the part after # the :, which is in addr.call. if addr.call: name = addr.call parent, obj = self.resolve(name, module) if (isclass(parent) and getattr(parent, '__module__', None) != module.__name__ and not isinstance(obj, Failure)): parent = transplant_class(parent, module.__name__) obj = getattr(parent, obj.__name__) log.debug("parent %s obj %s module %s", parent, obj, module) if isinstance(obj, Failure): return suite([obj]) else: return suite( ContextList([self.makeTest(obj, parent)], context=parent)) else: if addr.module: try: if addr.filename is None: module = resolve_name(addr.module) else: self.config.plugins.beforeImport( addr.filename, addr.module) # FIXME: to support module.name names, # do what resolve-name does and keep trying to # import, popping tail of module into addr.call, # until we either get an import or run out of # module parts try: module = self.importer.importFromPath( addr.filename, addr.module) finally: self.config.plugins.afterImport( addr.filename, addr.module) except (KeyboardInterrupt, SystemExit): raise except: exc = sys.exc_info() return suite([ Failure(exc[0], exc[1], exc[2], address=addr.totuple()) ]) if addr.call: return self.loadTestsFromName(addr.call, module) else: return self.loadTestsFromModule(module, addr.filename, discovered=discovered) elif addr.filename: path = addr.filename if addr.call: package = getpackage(path) if package is None: return suite([ Failure(ValueError, "Can't find callable %s in file %s: " "file is not a python module" % (addr.call, path), address=addr.totuple()) ]) return self.loadTestsFromName(addr.call, module=package) else: if op_isdir(path): # In this case we *can* be lazy since we know # that each module in the dir will be fully # loaded before its tests are executed; we # also know that we're not going to be asked # to load from . and ./some_module.py *as part # of this named test load* return LazySuite(lambda: self.loadTestsFromDir(path)) elif op_isfile(path): return self.loadTestsFromFile(path) else: return suite([ Failure(OSError, "No such file %s" % path, address=addr.totuple()) ]) else: # just a function? what to do? I think it can only be # handled when module is not None return suite([ Failure(ValueError, "Unresolvable test name %s" % name, address=addr.totuple()) ])
def loadTestsFromDir(self, path): """Load tests from the directory at path. This is a generator -- each suite of tests from a module or other file is yielded and is expected to be executed before the next file is examined. """ log.debug("load from dir %s", path) plugins = self.config.plugins plugins.beforeDirectory(path) if self.config.addPaths: paths_added = add_path(path, self.config) entries = os.listdir(path) sort_list(entries, regex_last_key(self.config.testMatch)) for entry in entries: # this hard-coded initial-dot test will be removed: # http://code.google.com/p/python-nose/issues/detail?id=82 if entry.startswith('.'): continue entry_path = op_abspath(op_join(path, entry)) is_file = op_isfile(entry_path) wanted = False if is_file: is_dir = False wanted = self.selector.wantFile(entry_path) else: is_dir = op_isdir(entry_path) if is_dir: # this hard-coded initial-underscore test will be removed: # http://code.google.com/p/python-nose/issues/detail?id=82 if entry.startswith('_'): continue wanted = self.selector.wantDirectory(entry_path) is_package = ispackage(entry_path) # Python 3.3 now implements PEP 420: Implicit Namespace Packages. # As a result, it's now possible that parent paths that have a # segment with the same basename as our package ends up # in module.__path__. So we have to keep track of what we've # visited, and not-revisit them again. if wanted and not self._haveVisited(entry_path): self._addVisitedPath(entry_path) if is_file: plugins.beforeContext() if entry.endswith('.py'): yield self.loadTestsFromName(entry_path, discovered=True) else: yield self.loadTestsFromFile(entry_path) plugins.afterContext() elif is_package: # Load the entry as a package: given the full path, # loadTestsFromName() will figure it out yield self.loadTestsFromName(entry_path, discovered=True) else: # Another test dir in this one: recurse lazily yield self.suiteClass( lambda: self.loadTestsFromDir(entry_path)) tests = [] for test in plugins.loadTestsFromDir(path): tests.append(test) # TODO: is this try/except needed? try: if tests: yield self.suiteClass(tests) except (KeyboardInterrupt, SystemExit): raise except: yield self.suiteClass([Failure(*sys.exc_info())]) # pop paths if self.config.addPaths: for p in paths_added: remove_path(p) plugins.afterDirectory(path)
def loadTestsFromDir(self, path): """Load tests from the directory at path. This is a generator -- each suite of tests from a module or other file is yielded and is expected to be executed before the next file is examined. """ log.debug("load from dir %s", path) plugins = self.config.plugins plugins.beforeDirectory(path) if self.config.addPaths: paths_added = add_path(path, self.config) entries = os.listdir(path) entries.sort(lambda a, b: match_last(a, b, self.config.testMatch)) for entry in entries: # this hard-coded initial-dot test will be removed: # http://code.google.com/p/python-nose/issues/detail?id=82 if entry.startswith('.'): continue entry_path = op_abspath(op_join(path, entry)) is_file = op_isfile(entry_path) wanted = False if is_file: is_dir = False wanted = self.selector.wantFile(entry_path) else: is_dir = op_isdir(entry_path) if is_dir: # this hard-coded initial-underscore test will be removed: # http://code.google.com/p/python-nose/issues/detail?id=82 if entry.startswith('_'): continue wanted = self.selector.wantDirectory(entry_path) is_package = ispackage(entry_path) if wanted: if is_file: plugins.beforeContext() if entry.endswith('.py'): yield self.loadTestsFromName( entry_path, discovered=True) else: yield self.loadTestsFromFile(entry_path) plugins.afterContext() elif is_package: # Load the entry as a package: given the full path, # loadTestsFromName() will figure it out yield self.loadTestsFromName( entry_path, discovered=True) else: # Another test dir in this one: recurse lazily yield self.suiteClass( lambda: self.loadTestsFromDir(entry_path)) tests = [] for test in plugins.loadTestsFromDir(path): tests.append(test) # TODO: is this try/except needed? try: if tests: yield self.suiteClass(tests) except (KeyboardInterrupt, SystemExit): raise except: yield self.suiteClass([Failure(*sys.exc_info())]) # pop paths if self.config.addPaths: map(remove_path, paths_added) plugins.afterDirectory(path)