def load_tests(loader: TestLoader, standard_tests: TestSuite, pattern: Optional[str]) -> TestSuite: ret = TestSuite((standard_tests, )) for cls in tests: suite = loader.loadTestsFromTestCase(cls) ret.addTests(suite) return ret
def make_suites(suites: list, context: ChainMap): suite = TestSuite() for item in suites: # 编译数据 parameters = compile_data(item.get('parameters', [{}])) variables = compile_data(item.get('variables', {})) request_settings = compile_data(item.get('request', {})) # 参数化处理 for parameter in parameters(context): # 创建子上下文 child_context = context.new_child(parameter) # 将变量合并到子上下文中 child_context.update(variables(child_context)) # 创建 http 客户端 client = HttpClient(**request_settings(child_context)) # 添加集合 suite.addTest( make_suite(item, context=child_context, client=client)) return suite
def load_tests(loader, standard_tests, pattern): """ Generate a test suite of tests from several test classes. Specifically: - test_volume_attached_after_migration from LiveMigratateServerWithVolumeTests - test_format_and_mount_disks, test_live_migrate_server, test_verify_ephemeral_disks_mounted from LiveMigratateServerWithVolumeTests These tests are added in a specific order to the load_tests method to enforce run order. This run order will ensure that the instance generated during LiveMigratateServerWithVolumeTests setUpClass is setup and then migrated in the appropriate order for these tests. """ suite = TestSuite() # During the LiveMigratateServerWithVolumeTests setup an instance is # created that will be used for the tests in this test module suite.addTest( LiveMigratateServerWithVolumeTests("test_format_and_mount_disks")) # This test performs a live migrate on the instance suite.addTest( LiveMigratateServerWithVolumeTests("test_live_migrate_server")) suite.addTest( LiveMigratateServerWithVolumeTests( "test_verify_ephemeral_disks_mounted")) suite.addTest( LiveMigratateServerWithVolumeTests( "test_volume_attached_after_migration")) return suite
def run_test(path: str = 'testcase'): """ :param path: With default path testcase, the method will execute all testcases, otherwise it only execute the cases which in the specific path :return: test report """ report_name = "{}_{}".format(path, str(datetime.now().strftime("%Y%m%d%H%M"))) testsuits = TestSuite() if path == 'testcase': for dir in os.listdir(os.path.join(os.curdir, path)): testsuits.addTests( unittest.defaultTestLoader.discover(dir, pattern='*test.py', top_level_dir='testcase')) else: testsuits.addTests( unittest.defaultTestLoader.discover(path, pattern='*test.py', top_level_dir='testcase')) result = BeautifulReport(testsuits) result.report(filename=report_name, description=path, log_path='result') shutil.copy('result/%s.html' % report_name, 'result/test_api_ressult.html') if result.failure_count or result.error_count: #主动出发失败导致jenkins失败 raise Exception("主动失败,发送邮件")
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest(ResizeServerVolumeIntegrationTest( "test_resize_server_and_confirm")) suite.addTest(ResizeServerVolumeIntegrationTest( "test_volume_attached_after_resize")) return suite
def load_tests(loader, tests, pattern): suite = TestSuite() suite.addTests(load_testsuite(loader, dirname(__file__))) # Numba CUDA tests are located in a separate directory: cuda_dir = join(dirname(dirname(__file__)), 'cuda/tests') suite.addTests(loader.discover(cuda_dir)) return suite
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest( CreateServerVolumeIntegrationTest("test_attach_volume_to_server")) suite.addTest( CreateServerVolumeIntegrationTest("test_format_and_mount_volume")) return suite
def load_tests_from_classes(test_cases): suite = TestSuite() loader = TestLoader() for test_class in test_cases: tests = loader.loadTestsFromTestCase(test_class) suite.addTests(tests) return suite
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest(LiveMigratationServerTests("test_format_and_mount_disks")) suite.addTest(LiveMigratationServerTests("test_live_migrate_server")) suite.addTest( LiveMigratationServerTests("test_verify_ephemeral_disks_mounted")) return suite
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest(CreateServerVolumeBurnIn("test_create_server_burn_in")) suite.addTest(CreateServerVolumeBurnIn("test_create_volume_burn_in")) suite.addTest( CreateServerVolumeBurnIn("test_attach_volume_to_server_burn_in")) return suite
def suite(): suite = TestSuite() suite.addTest(SimpleKindAwareInsertTest()) suite.addTest(KindAwareInsertWithParentTest()) suite.addTest(SimpleKindAwareQueryTest()) suite.addTest(AncestorQueryTest()) return suite
def make_suite(model: dict, context: ChainMap = None, client: HttpClient = None) -> TestSuite: suite = TestSuite() suite.name = compile_data(model.get('name', ''))(context) for item in model.get('tests', []): # 编译数据 parameters = compile_data(item.get('parameters', [{}])) variables = compile_data(item.get('variables', {})) # 参数化处理 for parameter in parameters(context): # 创建子上下文 child_context = context.new_child(parameter) # 合并变量 child_context.update(variables(child_context)) # 创建测试用例 suite.addTest( make_testcase(item, client=client, context=child_context, parent=suite)) return suite
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest( ObjectStoragePersistentResources("test_create_persistent_container")) suite.addTest( ObjectStoragePersistentResources("test_create_persistent_object")) return suite
def runTest(): suite = TestSuite() suite.addTest(self.test) runner = _TestRunner() result = runner.run(suite) if result.err is not None: desc = result.err[1].message + "\n" + "".join(traceback.format_tb(result.err[2])) raise Exception(desc)
def build_suite(self, *args, **kwargs): suite = super(CustomizedRunner, self).build_suite(*args, **kwargs) filtered = TestSuite() for test in suite: testname = str(test) if '.tests.' in testname and self.package in testname: filtered.addTest(test) return filtered
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest(RebuildServerVolumeIntegrationTest( "test_rebuild_server")) suite.addTest(RebuildServerVolumeIntegrationTest( "test_volume_detached_after_rebuild")) suite.addTest(RebuildServerVolumeIntegrationTest( "test_reattach_volume_after_rebuild")) return suite
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest( MultipleContainersTest( "test_custom_container_present_after_image_create")) suite.addTest( MultipleContainersTest( "test_custom_container_present_after_image_delete")) return suite
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest(VerifyComputePersistentResources( "test_verify_persistent_servers_existance")) suite.addTest(VerifyComputePersistentResources( "test_can_ssh_into_persistent_servers")) suite.addTest(VerifyComputePersistentResources( "test_suspend_resume_persistent_server")) return suite
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest( DeactivateReactivateBFV( 'test_create_volume_server_from_deactivated_image_invalid')) suite.addTest( DeactivateReactivateBFV( 'test_create_volume_server_from_reactivated_image')) return suite
def _runTest(self): """method used to run a test.""" suite = TestSuite() suite.addTest(self.test) runner = _TestRunner() result = runner.run(suite) if result.err is not None: desc = str(result.err) + "\n" + \ "".join(traceback.format_tb(result.err[2])) raise Exception(desc)
def load_tests(loader, standard_tests, pattern): suite = TestSuite() suite.addTest( VerifyObjectStoragePersistentResources( "test_verify_persistent_container_existance")) suite.addTest( VerifyObjectStoragePersistentResources( "test_list_persistent_container_contents")) suite.addTest( VerifyObjectStoragePersistentResources("test_get_persistent_object")) return suite
def creat(): loadcase=TestLoader() testsuite=[] suite1=TestSuite() suite2=TestSuite() caseclass_dict={} def backcount(d): n=0 for i in d.values(): n+=i return n def caseclass_count(caselist): print 'dir caselist',caselist for casename in caselist: module=loadcase._get_module_from_name(casename) for name in dir(module): obj = getattr(module,name) if isinstance(obj, type) and issubclass(obj, case.TestCase): modeltestcases_list=getTestCaseNames(obj,'test') caseclass_dict[obj]=len(modeltestcases_list) return caseclass_dict
def _runTest(self): """Method used to run a test""" suite = TestSuite() suite.addTest(self.test) runner = _TestRunner() result = runner.run(suite) if result.err is not None: desc = str(result.err) + '\n' + ''.join(traceback.format_tb(result.err[2])) if isinstance(result.err[1], AssertionError): raise AssertionError(desc) else: raise Exception(desc)
def _get_suite(self, test_labels, discover_kwargs, extra_tests, methods): suite = TestSuite() for label in test_labels: kwargs = discover_kwargs.copy() tests = None label_as_path = os.path.abspath(label) # if a module, or "module.ClassName[.method_name]", just run those if not os.path.exists(label_as_path): tests = self.test_loader.loadTestsFromName(label) elif os.path.isdir(label_as_path) and not self.top_level: # Try to be a bit smarter than unittest about finding the # default top-level for a given directory path, to avoid # breaking relative imports. (Unittest's default is to set # top-level equal to the path, which means relative imports # will result in "Attempted relative import in non-package."). # We'd be happy to skip this and require dotted module paths # (which don't cause this problem) instead of file paths (which # do), but in the case of a directory in the cwd, which would # be equally valid if considered as a top-level module or as a # directory path, unittest unfortunately prefers the latter. top_level = label_as_path while True: init_py = os.path.join(top_level, '__init__.py') if os.path.exists(init_py): try_next = os.path.dirname(top_level) if try_next == top_level: # __init__.py all the way down? give up. break top_level = try_next continue break kwargs['top_level_dir'] = top_level if not (tests and tests.countTestCases()): # if no tests found, it's probably a package; try discovery tests = self.test_loader.discover(start_dir=label, **kwargs) # make unittest forget the top-level dir it calculated from this # run, to support running tests from two different top-levels. self.test_loader._top_level_dir = None tests = self.get_tests_defined_in_methods_or_none(tests, methods) if tests: suite.addTests(tests) for test in extra_tests: suite.addTest(test) return suite
def main(): dt_suite = doctest.DocTestSuite(test_finder=doctest.DocTestFinder( recurse=True)) dt_suite.countTestCases() dt_suite.debug() if pytest is None: suite = TestSuite() all_test_suites = unittest.defaultTestLoader.discover(start_dir="test") suite.addTests(tests=[all_test_suites, dt_suite]) logging.debug(vars(suite)) successful = TextTestRunner().run(suite).wasSuccessful() return 0 if successful else 1 else: pytest.main(plugins=[])
def get_tests_defined_in_methods_or_none(self, tests, methods): if not methods: return tests else: if isinstance(tests, TestSuite): returned_tests = [] for test in tests: returned_test = self.get_tests_defined_in_methods_or_none( test, methods) if returned_test: returned_tests.append(returned_test) return TestSuite(returned_tests) elif tests._testMethodName in methods: return tests else: return None
def _getTestSuite(self) -> TestSuite: """ Returns: A suite of all tests in the unit test directory """ modules: List[str] = self.__getTestableModuleNames() fSuite: TestSuite = TestSuite() for module in modules: try: fixedName: str = module.replace('/', '.') m = import_module(fixedName) fSuite.addTest(m.suite()) except (ValueError, Exception) as e: self.logger.error( f'Module import problem with: {module}: {e}') return fSuite
def main() -> int: args = _parse_args() suite = defaultTestLoader.discover(normcase(_TESTS), top_level_dir=normcase(_ROOT), pattern="*.py") names = {*_names(args.paths)} tests = (test for test in _tests(suite) if not names or test.__module__ in names) runner = TextTestRunner( verbosity=args.verbosity, failfast=args.fail, buffer=args.buffer, ) installHandler() r = runner.run(TestSuite(tests)) return not r.wasSuccessful()
def test_package(*package_names): tests = [] test_loader = TestLoader() for module_name, module in sys.modules.items(): for package_name in package_names: if module_name.startswith('{}.'.format(package_name)): module_tests = test_loader.loadTestsFromModule(module) module_tests = [ t for t in module_tests if is_test_suite_loaded(t) ] tests.extend(module_tests) break test_result = TextTestRunner(failfast=True, resultclass=TimedTextTestResult).run( TestSuite(tests)) if not test_result.wasSuccessful(): raise Exception('test failed')
def to_suite(self, tests): """ Creates the container TestSuite object and populate with the TestCase objects based on the given test config objects. :param tests: test case config objects :type tests: list[RunnableTestCaseInfo] :return: overall TestSuite object :rtype: TestSuite """ test_cases = list() for case_info in tests: try: test_cases.append(self.build_from_config(case_info=case_info)) except Exception as e: log.error("Testcase loading failed: %s" % e.message) continue return TestSuite(test_cases)