def test_load_tests(self): tests.write_tree_from_desc('''dir: t file: t/__init__.py import unittest def load_tests(test_loader, tests, ignore): # A simple way to reveal the side effect is to add more tests class TestLoadTest(unittest.TestCase): def test_in_load_test(self): self.assertTrue(True) tests.addTests(test_loader.loadTestsFromTestCase(TestLoadTest)) return tests class TestInit(unittest.TestCase): def test_in_init(self): self.assertTrue(True) file: t/test_not_discovered.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) ''') test_loader = self.make_test_loader() suite = test_loader.discoverTestsFromTree('t') self.assertEqual(2, suite.countTestCases()) self.assertEqual( ['t.TestInit.test_in_init', 't.TestLoadTest.test_in_load_test'], [t.id() for t in testtools.iterate_tests(suite)])
def test_regular_below_scripts(self): tests.write_tree_from_desc('''dir: t file: t/__init__.py dir: t/regular file: t/regular/__init__.py from sst import loaders discover = loaders.discoverRegularTests file: t/regular/test_foo.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) file: t/script.py raise AssertionError('Loading only, executing fails') ''') suite = self.discover('t') # Check which kind of tests have been discovered or we may miss regular # test cases seen as scripts. self.assertEqual(['t.regular.test_foo.Test.test_me', 't.script'], [t.id() for t in testtools.iterate_tests(suite)])
def load_tests_input_scenario_utils(*args): """ Wrapper for testscenarios to set the scenarios to avoid running a getattr on the CONF object at import. """ if getattr(args[0], "suiteClass", None) is not None: loader, standard_tests, pattern = args else: standard_tests, module, loader = args output = None scenario_utils = None try: scenario_utils = InputScenarioUtils() scenario_flavor = scenario_utils.scenario_flavors scenario_image = scenario_utils.scenario_images except (exc_lib.InvalidCredentials, TypeError): output = standard_tests finally: if scenario_utils: scenario_utils.clear_creds() if output is not None: return output for test in testtools.iterate_tests(standard_tests): setattr(test, "scenarios", testscenarios.multiply_scenarios(scenario_image, scenario_flavor)) return testscenarios.load_tests_apply_scenarios(*args)
def test_regular_below_scripts(self): tests.write_tree_from_desc('''dir: t file: t/__init__.py dir: t/regular file: t/regular/__init__.py from sst import loader import unittest discover = loader.discoverRegularTests class Test(unittest.TestCase): def test_in_init(self): self.assertTrue(True) file: t/regular/foo.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) file: t/script.py raise AssertionError('Loading only, executing fails') ''') test_loader = loader.TestLoader() suite = test_loader.discoverTests( 't', file_loader_class=loader.ScriptLoader, dir_loader_class=loader.ScriptDirLoader) # Check which kind of tests have been discovered or we may miss regular # test cases seen as scripts. self.assertEqual([ 't.regular.Test.test_in_init', 't.regular.foo.Test.test_me', 't.script' ], [t.id() for t in testtools.iterate_tests(suite)])
def test_load_tests(self): tests.write_tree_from_desc('''dir: t file: t/__init__.py import unittest def load_tests(test_loader, tests, ignore): # A simple way to reveal the side effect is to add more tests class TestLoadTest(unittest.TestCase): def test_in_load_test(self): self.assertTrue(True) tests.addTests(test_loader.loadTestsFromTestCase(TestLoadTest)) return tests class TestInit(unittest.TestCase): def test_in_init(self): self.assertTrue(True) file: t/test_not_discovered.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) ''') test_loader = self.make_test_loader() suite = test_loader.discoverTestsFromTree('t') self.assertEqual(2, suite.countTestCases()) self.assertEqual(['t.TestInit.test_in_init', 't.TestLoadTest.test_in_load_test'], [t.id() for t in testtools.iterate_tests(suite)])
def partition_tests(suite, count): # Keep the same interface as the original concurrencytest.partition_tests independent_old_modules = [ "pcs_test.tier0.test_constraints", "pcs_test.tier0.test_resource", "pcs_test.tier0.test_stonith", ] old_tests = [] # tests are not independent, cannot run in parallel old_tests_independent = {} # independent modules independent_tests = [] for test in iterate_tests(suite): module = test.__class__.__module__ if not ( module.startswith("pcs_test.tier0.test_") or module.startswith("pcs_test.tier0.cib_resource") ): independent_tests.append(test) elif module in independent_old_modules: if module not in old_tests_independent: old_tests_independent[module] = [] old_tests_independent[module].append(test) else: old_tests.append(test) return [old_tests, independent_tests] + list(old_tests_independent.values())
def test_scripts_below_regular(self): tests.write_tree_from_desc('''dir: t file: t/__init__.py file: t/foo.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) dir: t/scripts file: t/scripts/__init__.py from sst import loader discover = loader.discoverTestScripts file: t/scripts/script.py raise AssertionError('Loading only, executing fails') ''') test_loader = loader.TestLoader() suite = test_loader.discoverTests('t') self.assertEqual(2, suite.countTestCases()) # Check which kind of tests have been discovered or we may miss regular # test cases seen as scripts. self.assertEqual(['t.foo.Test.test_me', 't.scripts.script'], [t.id() for t in testtools.iterate_tests(suite)])
def split(test): for script in testtools.iterate_tests(test): for script in script.split(splits): backlog.put(script) for _ in procs: backlog.put(None) return procs
def load_tests_input_scenario_utils(*args): """ Wrapper for testscenarios to set the scenarios to avoid running a getattr on the CONF object at import. """ if getattr(args[0], 'suiteClass', None) is not None: loader, standard_tests, pattern = args else: standard_tests, module, loader = args output = None scenario_utils = None try: scenario_utils = InputScenarioUtils() scenario_flavor = scenario_utils.scenario_flavors scenario_image = scenario_utils.scenario_images except (exceptions.InvalidConfiguration, TypeError): output = standard_tests finally: if scenario_utils: scenario_utils.clear_creds() if output is not None: return output for test in testtools.iterate_tests(standard_tests): setattr( test, 'scenarios', testscenarios.multiply_scenarios(scenario_image, scenario_flavor)) return testscenarios.load_tests_apply_scenarios(*args)
def test_custom_suite_without_sort_tests_works(self): a = PlaceHolder('a') b = PlaceHolder('b') class Subclass(unittest.TestSuite):pass input_suite = Subclass([b, a]) suite = sorted_tests(input_suite) self.assertEqual([b, a], list(iterate_tests(suite))) self.assertEqual([input_suite], list(iter(suite)))
def _iter_tests(names): """ Given a list of names, iterate through all the tests in them. """ for name in names: suite = _load_tests(name) for test in iterate_tests(suite): yield test
def test_load_tests_apply_scenarios_old_style(self): """Call load_tests in the way used by bzr.""" suite = load_tests_apply_scenarios( [self.SampleTest('test_nothing')], self.__class__.__module__, unittest.TestLoader(), ) result_tests = list(testtools.iterate_tests(suite)) self.assertEquals(2, len(result_tests), result_tests)
def test_load_tests_apply_scenarios(self): suite = load_tests_apply_scenarios( unittest.TestLoader(), [self.SampleTest('test_nothing')], None) result_tests = list(testtools.iterate_tests(suite)) self.assertEquals( 2, len(result_tests), result_tests)
def test_sorts_custom_suites(self): a = PlaceHolder('a') b = PlaceHolder('b') class Subclass(unittest.TestSuite): def sort_tests(self): self._tests = sorted_tests(self, True) input_suite = Subclass([b, a]) suite = sorted_tests(input_suite) self.assertEqual([a, b], list(iterate_tests(suite))) self.assertEqual([input_suite], list(iter(suite)))
def partition_tests(suite, count): """Partition suite into count lists of tests.""" # This just assigns tests in a round-robin fashion. On one hand this # splits up blocks of related tests that might run faster if they shared # resources, but on the other it avoids assigning blocks of slow tests to # just one partition. So the slowest partition shouldn't be much slower # than the fastest. partitions = [list() for _ in range(count)] tests = iterate_tests(suite) for partition, test in zip(cycle(partitions), tests): partition.append(test) return partitions
def test_load_tests_apply_scenarios_old_style(self): """Call load_tests in the way used by bzr.""" suite = load_tests_apply_scenarios( [self.SampleTest('test_nothing')], self.__class__.__module__, unittest.TestLoader(), ) result_tests = list(testtools.iterate_tests(suite)) self.assertEquals( 2, len(result_tests), result_tests)
def load_tests(*args): """ Wrapper for testscenarios to set the mandatory scenarios variable only in case a real test loader is in place. Will be automatically called in case the variable "load_tests" is set. """ if getattr(args[0], "suiteClass", None) is not None: loader, standard_tests, pattern = args else: standard_tests, module, loader = args for test in testtools.iterate_tests(standard_tests): schema_file = getattr(test, "_schema_file", None) if schema_file is not None: setattr(test, "scenarios", NegativeAutoTest.generate_scenario(schema_file)) return testscenarios.load_tests_apply_scenarios(*args)
def load_tests(*args): """ Wrapper for testscenarios to set the mandatory scenarios variable only in case a real test loader is in place. Will be automatically called in case the variable "load_tests" is set. """ if getattr(args[0], 'suiteClass', None) is not None: loader, standard_tests, pattern = args else: standard_tests, module, loader = args for test in testtools.iterate_tests(standard_tests): schema = getattr(test, '_schema', None) if schema is not None: setattr(test, 'scenarios', NegativeAutoTest.generate_scenario(schema)) return testscenarios.load_tests_apply_scenarios(*args)
def test_regular_and_scripts_mixed(self): def regular(dir_name, name, suffix=None): if suffix is None: suffix = '' return ''' file: {dir_name}/{name}{suffix} from sst import cases class Test_{name}(cases.SSTTestCase): def test_{name}(self): pass '''.format(**locals()) tests.write_tree_from_desc('''dir: tests file: tests/__init__.py from sst import loader discover = loader.discoverRegularTests ''') tests.write_tree_from_desc(regular('tests', 'test_real', '.py')) tests.write_tree_from_desc(regular('tests', 'test_real1', '.py')) tests.write_tree_from_desc(regular('tests', 'test_real2', '.py')) # Leading '_' => ignored tests.write_tree_from_desc(regular('tests', '_hidden', '.py')) # Not a python file => ignored tests.write_tree_from_desc(regular('tests', 'not-python')) # Some empty files tests.write_tree_from_desc(''' file: script1.py file: script2.py file: not_a_test # '.p' is intentional, not a typoed '.py' file: test_not_a_test.p _hidden_too.py ''') test_loader = loader.TestLoader() suite = test_loader.discoverTests( '.', file_loader_class=loader.ScriptLoader, dir_loader_class=loader.ScriptDirLoader) self.assertEqual(['script1', 'script2', 'tests.test_real.Test_test_real.test_test_real', 'tests.test_real1.Test_test_real1.test_test_real1', 'tests.test_real2.Test_test_real2.test_test_real2'], [t.id() for t in testtools.iterate_tests(suite)])
def test_regular_and_scripts_mixed(self): def regular(dir_name, name, suffix=None): if suffix is None: suffix = '' return ''' file: {dir_name}/{name}{suffix} from sst import cases class Test_{name}(cases.SSTTestCase): def test_{name}(self): pass '''.format(**locals()) tests.write_tree_from_desc('''dir: tests file: tests/__init__.py from sst import loader discover = loader.discoverRegularTests ''') tests.write_tree_from_desc(regular('tests', 'test_real', '.py')) tests.write_tree_from_desc(regular('tests', 'test_real1', '.py')) tests.write_tree_from_desc(regular('tests', 'test_real2', '.py')) # Leading '_' => ignored tests.write_tree_from_desc(regular('tests', '_hidden', '.py')) # Not a python file => ignored tests.write_tree_from_desc(regular('tests', 'not-python')) # Some empty files tests.write_tree_from_desc(''' file: script1.py file: script2.py file: not_a_test # '.p' is intentional, not a typoed '.py' file: test_not_a_test.p _hidden_too.py ''') test_loader = loader.TestLoader() suite = test_loader.discoverTests( '.', file_loader_class=loader.ScriptLoader, dir_loader_class=loader.ScriptDirLoader) self.assertEqual([ 'script1', 'script2', 'tests.test_real.Test_test_real.test_test_real', 'tests.test_real1.Test_test_real1.test_test_real1', 'tests.test_real2.Test_test_real2.test_test_real2' ], [t.id() for t in testtools.iterate_tests(suite)])
def partition_tests(suite, count): # Keep tests from the same class together but allow tests from modules # to go to different processes to aid parallelisation. modules = {} for test in iterate_tests(suite): m = test.__module__ + "." + test.__class__.__name__ if m not in modules: modules[m] = [] modules[m].append(test) # Simply divide the test blocks between the available processes partitions = [list() for _ in range(count)] for partition, m in zip(cycle(partitions), modules): partition.extend(modules[m]) # No point in empty threads so drop them return [p for p in partitions if p]
def load_tests_input_scenario_utils(*args): """ Wrapper for testscenarios to set the scenarios to avoid running a getattr on the CONF object at import. """ if getattr(args[0], 'suiteClass', None) is not None: loader, standard_tests, pattern = args else: standard_tests, module, loader = args scenario_utils = InputScenarioUtils() scenario_flavor = scenario_utils.scenario_flavors scenario_image = scenario_utils.scenario_images for test in testtools.iterate_tests(standard_tests): setattr(test, 'scenarios', testscenarios.multiply_scenarios( scenario_image, scenario_flavor)) return testscenarios.load_tests_apply_scenarios(*args)
def load_tests_input_scenario_utils(*args): """ Wrapper for testscenarios to set the scenarios to avoid running a getattr on the CONF object at import. """ if getattr(args[0], 'suiteClass', None) is not None: loader, standard_tests, pattern = args else: standard_tests, module, loader = args scenario_utils = InputScenarioUtils() scenario_flavor = scenario_utils.scenario_flavors scenario_image = scenario_utils.scenario_images for test in testtools.iterate_tests(standard_tests): setattr( test, 'scenarios', testscenarios.multiply_scenarios(scenario_image, scenario_flavor)) return testscenarios.load_tests_apply_scenarios(*args)
def generate_scenarios(test_or_suite): """Yield the tests in test_or_suite with scenario multiplication done. TestCase objects with no scenarios specified are yielded unaltered. Tests with scenarios are not yielded at all, instead the results of multiplying them by the scenarios they specified gets yielded. :param test_or_suite: A TestCase or TestSuite. :return: A generator of tests - objects satisfying the TestCase protocol. """ for test in iterate_tests(test_or_suite): scenarios = getattr(test, 'scenarios', None) if scenarios: for newtest in apply_scenarios(scenarios, test): newtest.scenarios = None yield newtest else: yield test
def test_discover_changing_file_matcher(self): tests.write_tree_from_desc('''dir: t file: t/__init__.py file: t/test_foo.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) dir: t/other file: t/other/__init__.py import unittest from sst import loaders def discover(test_loader, package, directory_path, names): suite = test_loader.loadTestsFromModule(package) # Change the test.*\.py rule fmatcher = loaders.NameMatcher(includes=['.*']) with loaders.NameMatchers(test_loader, fmatcher) as tl: suite.addTests(tl.discoverTestsFromNames(directory_path, names)) return suite class Test(unittest.TestCase): def test_in_init(self): self.assertTrue(True) file: t/other/not_starting_with_test.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) ''') test_loader = self.make_test_loader() suite = test_loader.discoverTestsFromTree('t') self.assertEqual(3, suite.countTestCases()) self.assertEqual([ 't.other.Test.test_in_init', 't.other.not_starting_with_test.Test.test_me', 't.test_foo.Test.test_me' ], [t.id() for t in testtools.iterate_tests(suite)])
def test_discover_changing_file_matcher(self): tests.write_tree_from_desc('''dir: t file: t/__init__.py file: t/test_foo.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) dir: t/other file: t/other/__init__.py import unittest from sst import loaders def discover(test_loader, package, directory_path, names): suite = test_loader.loadTestsFromModule(package) # Change the test.*\.py rule fmatcher = loaders.NameMatcher(includes=['.*']) with loaders.NameMatchers(test_loader, fmatcher) as tl: suite.addTests(tl.discoverTestsFromNames(directory_path, names)) return suite class Test(unittest.TestCase): def test_in_init(self): self.assertTrue(True) file: t/other/not_starting_with_test.py import unittest class Test(unittest.TestCase): def test_me(self): self.assertTrue(True) ''') test_loader = self.make_test_loader() suite = test_loader.discoverTestsFromTree('t') self.assertEqual(3, suite.countTestCases()) self.assertEqual(['t.other.Test.test_in_init', 't.other.not_starting_with_test.Test.test_me', 't.test_foo.Test.test_me'], [t.id() for t in testtools.iterate_tests(suite)])
def split_suite(self, suite): tests = list(enumerate(iterate_tests(suite))) return [(test, _u(str(pos))) for pos, test in tests]
def test_load_tests_apply_scenarios(self): suite = load_tests_apply_scenarios(unittest.TestLoader(), [self.SampleTest('test_nothing')], None) result_tests = list(testtools.iterate_tests(suite)) self.assertEquals(2, len(result_tests), result_tests)
def split_suite(self, suite): tests = list(iterate_tests(suite)) return tests[0], tests[1]
def assertFiltered(self, expected, regexps, ids): """Check that ``regexps`` filters tests created from ``ids``.""" filtered = filters.include_regexps(regexps, create_tests_from_ids(ids)) self.assertEqual(expected, [t.id() for t in testtools.iterate_tests(filtered)])
def split_suite(self, suite): return list(iterate_tests(suite))
def test_sorts_simple_suites(self): a = PlaceHolder('a') b = PlaceHolder('b') suite = sorted_tests(unittest.TestSuite([b, a])) self.assertEqual([a, b], list(iterate_tests(suite)))
def split_suite(self, suite): tests = list(enumerate(iterate_tests(suite))) return [(test, str(pos)) for pos, test in tests]
def test_num_tests(self): num_tests = len(list(iterate_tests(self.suite))) self.assertEqual(num_tests, 8)
def assertFiltered(self, expected, condition, ids): """Check that ``condition`` filters tests created from ``ids``.""" filtered = filters.filter_suite(condition, create_tests_from_ids(ids)) self.assertEqual(expected, [t.id() for t in testtools.iterate_tests(filtered)])