def __init__(self, module=__name__, defaultTest=None, argv=None, testRunner=None, testLoader=defaultTestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, stdout=None): if module == __name__: self.module = None elif istext(module): self.module = __import__(module) for part in module.split('.')[1:]: self.module = getattr(self.module, part) else: self.module = module if argv is None: argv = sys.argv if stdout is None: stdout = sys.stdout self.exit = exit self.failfast = failfast self.catchbreak = catchbreak self.verbosity = verbosity self.buffer = buffer self.defaultTest = defaultTest self.listtests = False self.load_list = None self.testRunner = testRunner self.testLoader = testLoader progName = argv[0] if progName.endswith('%srun.py' % os.path.sep): elements = progName.split(os.path.sep) progName = '%s.run' % elements[-2] else: progName = os.path.basename(argv[0]) self.progName = progName self.parseArgs(argv) if self.load_list: # TODO: preserve existing suites (like testresources does in # OptimisingTestSuite.add, but with a standard protocol). # This is needed because the load_tests hook allows arbitrary # suites, even if that is rarely used. source = file(self.load_list, 'rb') try: lines = source.readlines() finally: source.close() test_ids = set(line.strip() for line in lines) filtered = unittest.TestSuite() for test in iterate_tests(self.test): if test.id() in test_ids: filtered.addTest(test) self.test = filtered if not self.listtests: self.runTests() else: for test in iterate_tests(self.test): stdout.write('%s\n' % test.id())
def __init__(self, module=__name__, defaultTest=None, argv=None, testRunner=None, testLoader=defaultTestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, stdout=None): if module == __name__: self.module = None elif istext(module): self.module = __import__(module) for part in module.split('.')[1:]: self.module = getattr(self.module, part) else: self.module = module if argv is None: argv = sys.argv if stdout is None: stdout = sys.stdout self.exit = exit self.failfast = failfast self.catchbreak = catchbreak self.verbosity = verbosity self.buffer = buffer self.defaultTest = defaultTest self.listtests = False self.load_list = None self.testRunner = testRunner self.testLoader = testLoader progName = argv[0] if progName.endswith('%srun.py' % os.path.sep): elements = progName.split(os.path.sep) progName = '%s.run' % elements[-2] else: progName = os.path.basename(argv[0]) self.progName = progName self.parseArgs(argv) if self.load_list: # TODO: preserve existing suites (like testresources does in # OptimisingTestSuite.add, but with a standard protocol). # This is needed because the load_tests hook allows arbitrary # suites, even if that is rarely used. source = open(self.load_list, 'rb') try: lines = source.readlines() finally: source.close() test_ids = set(line.strip().decode('utf-8') for line in lines) filtered = unittest.TestSuite() for test in iterate_tests(self.test): if test.id() in test_ids: filtered.addTest(test) self.test = filtered if not self.listtests: self.runTests() else: for test in iterate_tests(self.test): stdout.write('%s\n' % test.id())
def list_test(test): """Return the test ids that would be run if test() was run. When things fail to import they can be represented as well, though we use an ugly hack (see http://bugs.python.org/issue19746 for details) to determine that. The difference matters because if a user is filtering tests to run on the returned ids, a failed import can reduce the visible tests but it can be impossible to tell that the selected test would have been one of the imported ones. :return: A tuple of test ids that would run and error strings describing things that failed to import. """ unittest_import_strs = { 'unittest2.loader.ModuleImportFailure.', 'unittest.loader.ModuleImportFailure.', 'discover.ModuleImportFailure.' } test_ids = [] errors = [] for test in iterate_tests(test): # Much ugly. for prefix in unittest_import_strs: if test.id().startswith(prefix): errors.append(test.id()[len(prefix):]) break else: test_ids.append(test.id()) return test_ids, errors
def list_test(test): """Return the test ids that would be run if test() was run. When things fail to import they can be represented as well, though we use an ugly hack (see http://bugs.python.org/issue19746 for details) to determine that. The difference matters because if a user is filtering tests to run on the returned ids, a failed import can reduce the visible tests but it can be impossible to tell that the selected test would have been one of the imported ones. :return: A tuple of test ids that would run and error strings describing things that failed to import. """ unittest_import_strs = set([ 'unittest2.loader.ModuleImportFailure.', 'unittest.loader.ModuleImportFailure.', 'discover.ModuleImportFailure.' ]) test_ids = [] errors = [] for test in iterate_tests(test): # Much ugly. for prefix in unittest_import_strs: if test.id().startswith(prefix): errors.append(test.id()[len(prefix):]) break else: test_ids.append(test.id()) return test_ids, errors
def discover_stress_tests(path="./", filter_attr=None, call_inherited=False): """Discovers all tempest tests and create action out of them """ LOG.info("Start test discovery") tests = [] testloader = loader.TestLoader() list = testloader.discover(path) for func in testsuite.iterate_tests(list): attrs = [] try: method_name = getattr(func, "_testMethodName") full_name = "%s.%s.%s" % (func.__module__, func.__class__.__name__, method_name) test_func = getattr(func, method_name) # NOTE(mkoderer): this contains a list of all type attributes attrs = getattr(test_func, "__testtools_attrs") except Exception: next if "stress" in attrs: if filter_attr is not None and filter_attr not in attrs: continue class_setup_per = getattr(test_func, "st_class_setup_per") action = { "action": "tempest.stress.actions.unit_test.UnitTest", "kwargs": {"test_method": full_name, "class_setup_per": class_setup_per}, } if not call_inherited and getattr(test_func, "st_allow_inheritance") is not True: class_structure = inspect.getmro(test_func.im_class) if test_func.__name__ not in class_structure[0].__dict__: continue tests.append(action) return tests
def discover_stress_tests(path="./", filter_attr=None, call_inherited=False): """Discovers all tempest tests and create action out of them """ LOG.info("Start test discovery") tests = [] testloader = loader.TestLoader() list = testloader.discover(path) for func in (testsuite.iterate_tests(list)): attrs = [] try: method_name = getattr(func, '_testMethodName') full_name = "%s.%s.%s" % (func.__module__, func.__class__.__name__, method_name) test_func = getattr(func, method_name) # NOTE(mkoderer): this contains a list of all type attributes attrs = getattr(test_func, "__testtools_attrs") except Exception: next if 'stress' in attrs: if filter_attr is not None and filter_attr not in attrs: continue class_setup_per = getattr(test_func, "st_class_setup_per") action = { 'action': "tempest.stress.actions.unit_test.UnitTest", 'kwargs': { "test_method": full_name, "class_setup_per": class_setup_per } } if (not call_inherited and getattr( test_func, "st_allow_inheritance") is not True): class_structure = inspect.getmro(test_func.im_class) if test_func.__name__ not in class_structure[0].__dict__: continue tests.append(action) return tests
def __init__(self, module=__name__, defaultTest=None, argv=None, testRunner=None, testLoader=defaultTestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, stdout=None, tb_locals=False): if module == __name__: self.module = None elif isinstance(module, str): self.module = __import__(module) for part in module.split('.')[1:]: self.module = getattr(self.module, part) else: self.module = module if argv is None: argv = sys.argv if stdout is None: stdout = sys.stdout self.stdout = stdout self.exit = exit self.failfast = failfast self.catchbreak = catchbreak self.verbosity = verbosity self.buffer = buffer self.tb_locals = tb_locals self.defaultTest = defaultTest # XXX: Local edit (see http://bugs.python.org/issue22860) self.listtests = False self.load_list = None self.testRunner = testRunner self.testLoader = testLoader progName = argv[0] if progName.endswith('%srun.py' % os.path.sep): elements = progName.split(os.path.sep) progName = '%s.run' % elements[-2] else: progName = os.path.basename(argv[0]) self.progName = progName self.parseArgs(argv) # XXX: Local edit (see http://bugs.python.org/issue22860) if self.load_list: # TODO: preserve existing suites (like testresources does in # OptimisingTestSuite.add, but with a standard protocol). # This is needed because the load_tests hook allows arbitrary # suites, even if that is rarely used. source = open(self.load_list, 'rb') try: lines = source.readlines() finally: source.close() test_ids = {line.strip().decode('utf-8') for line in lines} self.test = filter_by_ids(self.test, test_ids) # XXX: Local edit (see http://bugs.python.org/issue22860) if not self.listtests: self.runTests() else: runner = self._get_runner() if safe_hasattr(runner, 'list'): try: runner.list(self.test, loader=self.testLoader) except TypeError: runner.list(self.test) else: for test in iterate_tests(self.test): self.stdout.write('%s\n' % test.id()) del self.testLoader.errors[:]
def get_all_tests(self): tests = TestLoader().discover(self.testroot) return [test for test in iterate_tests(tests)]
def __init__(self, module=__name__, defaultTest=None, argv=None, testRunner=None, testLoader=defaultTestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, stdout=None, tb_locals=False): if module == __name__: self.module = None elif istext(module): self.module = __import__(module) for part in module.split('.')[1:]: self.module = getattr(self.module, part) else: self.module = module if argv is None: argv = sys.argv if stdout is None: stdout = sys.stdout self.stdout = stdout self.exit = exit self.failfast = failfast self.catchbreak = catchbreak self.verbosity = verbosity self.buffer = buffer self.tb_locals = tb_locals self.defaultTest = defaultTest # XXX: Local edit (see http://bugs.python.org/issue22860) self.listtests = False self.load_list = None self.testRunner = testRunner self.testLoader = testLoader progName = argv[0] if progName.endswith('%srun.py' % os.path.sep): elements = progName.split(os.path.sep) progName = '%s.run' % elements[-2] else: progName = os.path.basename(argv[0]) self.progName = progName self.parseArgs(argv) # XXX: Local edit (see http://bugs.python.org/issue22860) if self.load_list: # TODO: preserve existing suites (like testresources does in # OptimisingTestSuite.add, but with a standard protocol). # This is needed because the load_tests hook allows arbitrary # suites, even if that is rarely used. source = open(self.load_list, 'rb') try: lines = source.readlines() finally: source.close() test_ids = set(line.strip().decode('utf-8') for line in lines) self.test = filter_by_ids(self.test, test_ids) # XXX: Local edit (see http://bugs.python.org/issue22860) if not self.listtests: self.runTests() else: runner = self._get_runner() if safe_hasattr(runner, 'list'): try: runner.list(self.test, loader=self.testLoader) except TypeError: runner.list(self.test) else: for test in iterate_tests(self.test): self.stdout.write('%s\n' % test.id()) del self.testLoader.errors[:]
def list(self, test): """List the tests that would be run if test() was run.""" for test in iterate_tests(test): self.stdout.write('%s\n' % test.id())