def _module_iter(self, filename): """Returns an iterator of Test objects for the contents of the given python module file. """ try: fname, mod = get_module(filename) except: t = Test(filename, self.options) t.status = 'FAIL' t.err_msg = traceback.format_exc() yield t else: if basename(fname).startswith('__init__.'): for result in self._dir_iter(dirname(fname)): yield result else: for name, obj in getmembers(mod): if isclass(obj) and issubclass(obj, TestCase): for result in self._testcase_iter(filename, obj): yield result elif isfunction(obj) and self.func_match(name): yield Test(':'.join((filename, obj.__name__)), self.options)
def _testspec_iter(self, testspec): """Returns an iterator of Test objects found in the module/testcase/method specified in testspec. The format of testspec is one of the following: <module> <module>:<testcase> <module>:<testcase>.<method> <module>:<function> where <module> is either the python module path or the actual file system path to the .py file. """ module, _, rest = testspec.partition(':') if rest: tcasename, _, method = rest.partition('.') if method: yield Test(testspec) else: # could be a test function or a TestCase try: fname, mod = get_module(module) except: yield Test(testspec, 'FAIL', err_msg=traceback.format_exc()) return try: tcase = get_testcase(fname, mod, tcasename) except (AttributeError, TypeError): yield Test(testspec) else: for spec in self._testcase_iter(fname, tcase): yield spec else: for spec in self._module_iter(module): yield spec
def _parse_test_path(testspec): """Return a tuple of the form (module, testcase, func) based on the given testspec. The format of testspec is one of the following: <module> <module>:<testcase> <module>:<testcase>.<method> <module>:<function> where <module> is either the python module path or the actual file system path to the .py file. A value of None in the tuple indicates that that part of the testspec was not present. """ testcase = method = None testspec = testspec.strip() parts = testspec.split(':') if len(parts) > 1 and parts[1].startswith('\\'): # windows abs path module = ':'.join(parts[:2]) if len(parts) == 3: rest = parts[2] else: rest = '' else: module, _, rest = testspec.partition(':') _, mod = get_module(module) if rest: objname, _, method = rest.partition('.') obj = getattr(mod, objname) if inspect.isclass(obj) and issubclass(obj, unittest.TestCase): testcase = obj if method: meth = getattr(obj, method) if not ismethod(meth): raise TypeError("'%s' is not a method." % rest) elif isinstance(obj, FunctionType): method = obj.__name__ else: raise TypeError("'%s' is not a TestCase or a function." % objname) return (mod, testcase, method)
def _parse_test_path(testspec): """Return a tuple of the form (module, testcase, func) based on the given testspec. The format of testspec is one of the following: <module> <module>:<testcase> <module>:<testcase>.<method> <module>:<function> where <module> is either the python module path or the actual file system path to the .py file. A value of None in the tuple indicates that that part of the testspec was not present. """ testcase = funcname = tcasename = None testspec = testspec.strip() parts = testspec.split(':') if len(parts) > 1 and parts[1].startswith('\\'): # windows abs path module = ':'.join(parts[:2]) if len(parts) == 3: rest = parts[2] else: rest = '' else: module, _, rest = testspec.partition(':') _, mod = get_module(module) if rest: objname, _, funcname = rest.partition('.') obj = getattr(mod, objname) if isclass(obj) and issubclass(obj, TestCase): tcasename = objname testcase = obj if funcname: meth = getattr(obj, funcname) if not ismethod(meth): raise TypeError("'%s' is not a method." % rest) elif isinstance(obj, FunctionType): funcname = obj.__name__ else: raise TypeError("'%s' is not a TestCase or a function." % objname) return (mod, tcasename, funcname)
def _module_iter(self, filename): """Returns an iterator of Test objects for the contents of the given python module file. """ try: fname, mod = get_module(filename) except: yield Test(filename, 'FAIL', err_msg=traceback.format_exc()) else: if basename(fname).startswith(six.text_type('__init__.')): for result in self._dir_iter(dirname(fname)): yield result else: for name, obj in getmembers(mod): if isclass(obj) and issubclass(obj, TestCase): for result in self._testcase_iter(filename, obj): yield result elif isfunction(obj) and fnmatchcase(name, self.func_pattern): yield Test(':'.join((filename, obj.__name__)))
def _testspec_iter(self, testspec): """Returns an iterator of Test objects found in the module/testcase/method specified in testspec. The format of testspec is one of the following: <module> <module>:<testcase> <module>:<testcase>.<method> <module>:<function> where <module> is either the python module path or the actual file system path to the .py file. """ module, _, rest = testspec.partition(':') if rest: tcasename, _, method = rest.partition('.') if method: yield Test(testspec, self.options) else: # could be a test function or a TestCase try: fname, mod = get_module(module) except: t = Test(testspec, self.options) t.status = 'FAIL' t.err_msg = traceback.format_exc() return try: tcase = get_testcase(fname, mod, tcasename) except (AttributeError, TypeError): t = Test(testspec, self.options) t.status = 'FAIL' t.err_msg = traceback.format_exc() yield t else: for test in self._testcase_iter(fname, tcase): yield test else: for test in self._module_iter(module): yield test
def _module_iter(self, filename): """Iterate over all testspecs in a module.""" try: fname, mod = get_module(filename) except: yield TestResult(filename, 0, 0, 'FAIL', traceback.format_exc()) else: if basename(fname).startswith(six.text_type('__init__.')): for result in self._dir_iter(dirname(fname)): yield result else: for name, obj in inspect.getmembers(mod): if inspect.isclass(obj): if issubclass(obj, unittest.TestCase): for result in self._testcase_iter(filename, obj): yield result elif inspect.isfunction(obj): if fnmatch(name, self.func_pattern): yield ':'.join((filename, obj.__name__))