我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用unittest.findTestCases()。
def run_unittest(*classes): """Run tests from unittest.TestCase-derived classes.""" valid_types = (unittest.TestSuite, unittest.TestCase) suite = unittest.TestSuite() for cls in classes: if isinstance(cls, str): if cls in sys.modules: suite.addTest(unittest.findTestCases(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: suite.addTest(unittest.makeSuite(cls)) _run_suite(suite) #======================================================================= # doctest driver.
def run_unittest(*classes): """Run tests from unittest.TestCase-derived classes.""" valid_types = (unittest.TestSuite, unittest.TestCase) suite = unittest.TestSuite() for cls in classes: if isinstance(cls, str): if cls in sys.modules: suite.addTest(unittest.findTestCases(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: suite.addTest(unittest.makeSuite(cls)) _run_suite(suite) #======================================================================= # Check for the presence of docstrings.
def test_suite(package=__package__, directory=os.path.dirname(__file__)): suite = unittest.TestSuite() for name in os.listdir(directory): if name.startswith(('.', '__')): continue path = os.path.join(directory, name) if (os.path.isfile(path) and name.startswith('test_') and name.endswith('.py')): submodule_name = os.path.splitext(name)[0] module_name = "{0}.{1}".format(package, submodule_name) __import__(module_name, level=0) module_tests = unittest.findTestCases(sys.modules[module_name]) suite.addTest(module_tests) elif os.path.isdir(path): package_name = "{0}.{1}".format(package, name) __import__(package_name, level=0) package_tests = getattr(sys.modules[package_name], 'test_suite')() suite.addTest(package_tests) else: continue return suite
def collect_tests(): tests = [] for root, _, files in os.walk(TESTS_ROOT): for f in files: if TEST_MODULE_REGEX.match(f): test_file = os.path.join(root, f) module_name, _ = os.path.splitext(os.path.relpath(test_file, PROJECT_ROOT)) module_name = module_name.replace("/", ".") module = __import__(module_name) for namepart in module_name.split(".")[1:]: module = getattr(module, namepart) for test_class in unittest.findTestCases(module)._tests: for test_function in test_class._tests: test_full_name = ".".join([module_name, type(test_function).__name__, test_function._testMethodName]) tests.append((test_file, module_name, test_full_name)) return tests
def run_unittest(*classes): """Run tests from unittest.TestCase-derived classes.""" valid_types = (unittest.TestSuite, unittest.TestCase) suite = unittest.TestSuite() for cls in classes: if isinstance(cls, str): if cls in sys.modules: suite.addTest(unittest.findTestCases(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: suite.addTest(unittest.makeSuite(cls)) suite = filter_maybe(suite) _run_suite(suite) #======================================================================= # Check for the presence of docstrings.
def find(): svn = re.compile('\.svn') test = re.compile('test.+\.py$') alltests = unittest.TestSuite() for path, _, files in os.walk(here): if svn.search(path): continue for f in files: if test.search(f): module = __import__(f.split('.')[0]) alltests.addTest(unittest.findTestCases(module)) return alltests
def run_unittest(*classes): """Run tests from unittest.TestCase-derived classes.""" valid_types = (unittest.TestSuite, unittest.TestCase) suite = unittest.TestSuite() for cls in classes: if isinstance(cls, str): if cls in sys.modules: suite.addTest(unittest.findTestCases(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: suite.addTest(unittest.makeSuite(cls)) def case_pred(test): if match_tests is None: return True for name in test.id().split("."): if fnmatch.fnmatchcase(name, match_tests): return True return False _filter_suite(suite, case_pred) _run_suite(suite) # We don't have sysconfig on Py2.6: # #======================================================================= # # Check for the presence of docstrings. # # HAVE_DOCSTRINGS = (check_impl_detail(cpython=False) or # sys.platform == 'win32' or # sysconfig.get_config_var('WITH_DOC_STRINGS')) # # requires_docstrings = unittest.skipUnless(HAVE_DOCSTRINGS, # "test requires docstrings") # # # #======================================================================= # doctest driver.
def test_suite(): return unittest.findTestCases(sys.modules[__name__])
def run_unittest(*classes): """Run tests from unittest.TestCase-derived classes.""" valid_types = (unittest.TestSuite, unittest.TestCase) suite = unittest.TestSuite() for cls in classes: if isinstance(cls, str): if cls in sys.modules: suite.addTest(unittest.findTestCases(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: suite.addTest(unittest.makeSuite(cls)) def case_pred(test): if match_tests is None: return True for name in test.id().split("."): if fnmatch.fnmatchcase(name, match_tests): return True return False _filter_suite(suite, case_pred) _run_suite(suite) #======================================================================= # doctest driver.
def run_unittest(*classes): """Run tests from unittest.TestCase-derived classes.""" valid_types = (unittest.TestSuite, unittest.TestCase) suite = unittest.TestSuite() for cls in classes: if isinstance(cls, str): if cls in sys.modules: suite.addTest(unittest.findTestCases(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: suite.addTest(unittest.makeSuite(cls)) def case_pred(test): if match_tests is None: return True for name in test.id().split("."): if fnmatch.fnmatchcase(name, match_tests): return True return False _filter_suite(suite, case_pred) _run_suite(suite) #======================================================================= # Check for the presence of docstrings. # Rather than trying to enumerate all the cases where docstrings may be # disabled, we just check for that directly