def generate_test_cases(self, context_factory, parameterizations=None): """Creates the set of test cases that this test represents. The parameterizations argument should contain a parameterizations registry (keyed by parameterization name) containing values that are instances of a checkers.Parameterization class. Args: context_factory: Callable to create a context instance given a TestCase. parameterizations: (Registry) Parameterizations used to create test cases. Returns: list(TestCase): List of test cases (aka test closures). """ test_cases = registry.AutoKeyRegistry(lambda tc: tc.full_name) if not parameterizations: test_case = TestCase(self, context_factory, description=self.description) test_cases.register(test_case) return test_cases # It is a parameterized test, so we need to generate multiple test cases; # one for each parameterization. for suffix, param in parameterizations.iteritems(): name = '%s_%s' % (self.name, suffix) full_name = '%s_%s' % (self.full_name, suffix) test_case = TestCase(self, context_factory, name=name, full_name=full_name, description=self.description) for key, value in param.variables.iteritems(): test_case.context.variables.register(key, value) for suite_name in param.suites: test_case.test_suites.register(TestSuite(suite_name)) test_cases.register(test_case) return test_cases
def __init__(self, name, full_name, description): """Initializes a new instance of a test. Args: name: (string) The name of the test. full_name: (string) The fully-qualified name of the test. description: (string) A description of the test. """ self.name = unicode(name) self.full_name = unicode(full_name) self.description = unicode(description) self.decorator_parameterizations = registry.AutoKeyRegistry( lambda param: param.name) self.setup = registry.AutoKeyRegistry(lambda func: func.__name__) self.teardown = registry.AutoKeyRegistry(lambda func: func.__name__) self.test_suite_names = set()
def tests_from_module(module, include_imports=False): """Finds all of the tests defined in a module and puts them into a registry. Args: module: (module) The module that we want to look for tests in. include_imports: (bool) Whether to search for Tests in the impors, too. Returns: TestRegistry: The set of discovered tests. Raises: NotImplementedError: Feature doesn't exist to find tests from imports, yet. """ if include_imports: raise NotImplementedError( 'Searching for tests in imports not supported.') test_registry = registry.AutoKeyRegistry(lambda test: test.full_name) for attr_name in dir(module): attr = getattr(module, attr_name) if type(attr) is test.Test: # TODO(barkimedes): support tests that are definitions of test classes # rather than just instances. raise NotImplementedError('Cannot load Test class definitions') if isinstance(attr, test.Test): test_registry.register(attr) return test_registry
def generate_test_cases(self, context_factory=context.Context): """Generates the real test cases for the test run. A test case is essentially the closure of a test. So what this function does is take all of the tests that have been defined, creates test cases for them, and (for each test case), sets up what test suites it is a member of. It also adds all of the variables from the test run to each test case's individual context. All tests will be added to a global test suite. Args: context_factory: (function(test_case, test_run)) Creates a context. Returns: TestCaseRegistry: Registry containing all of the test cases for the run. """ global_suite = test_suite.TestSuite('all', 'Suite containing all tests.') test_case_registry = registry.AutoKeyRegistry(lambda tc: tc.full_name) for original_test in self.tests.values(): test = original_test.clone() # Add all of the parameterizations just on the test to the test in the # test run. for parameterization in test.decorator_parameterizations.values(): self.parameterizations.register(test.full_name, parameterization) # Make sure that the decorator setup functions are called closer to the # test than the test run's test case setup functions. test.setup.clear() test.setup.merge(self.test_case_setup) test.setup.merge(original_test.setup) test.teardown.merge(self.test_case_teardown) for suite_name in test.test_suite_names: self.test_suites[suite_name].register(test) new_context_factory = lambda test_case: context_factory( test_case, self) params = None if test.full_name in self.parameterizations: params = self.parameterizations[test.full_name] test_cases = test.generate_test_cases(new_context_factory, params) for test_case in test_cases.values(): # Replace the empty suite provided by parameterizations with the actual # suite from the test run. for suite_name in test_case.test_suites: self.test_suites[suite_name].register(test_case) test_case.test_suites.register( self.test_suites[suite_name]) test_case.test_suites.register(global_suite) for suite in self.test_suites.values(): if test.full_name in suite or test_case.full_name in suite: test_case.test_suites.register(suite) test_case_registry.register(test_case) for key, value in self.variables.iteritems(): test_case.context.variables.register(key, value) return test_case_registry
def __init__(self, name): """Initializes a new instance of a TestRun. Args: name: (string) The name to use for the test run. """ self.name = name self.tests = registry.AutoKeyRegistry(lambda test: test.full_name) self.variables = registry.Registry() self.setup = registry.AutoKeyRegistry(lambda func: func.__name__) self.teardown = registry.AutoKeyRegistry(lambda func: func.__name__) self.test_case_setup = registry.AutoKeyRegistry( lambda func: func.__name__) self.test_case_teardown = registry.AutoKeyRegistry( lambda func: func.__name__) self.test_suites = _TestRunSuiteRegistry(self) # Parameterizations are stored with the key as the test's full name and the # value is a parameterization registry. param_key = lambda param: param.name # pylint: disable=line-too-long parameterization_registry_factory = lambda: registry.AutoKeyRegistry( param_key) # pylint: enable=line-too-long self.parameterizations = registry.SuperRegistry( parameterization_registry_factory)
def __init__(self, test, context_factory, name=None, full_name=None, description=''): """Initializes a new instance of a TestCase. Args: test: (Test) The test that the test case provides closure for. context_factory: (function(test_case)) Function that creates a context. name: (string) The name of the test case. full_name: (string) The fully-qualified name of the test case. description: (string) A description of the test case. """ self.name = name if name else test.name self.full_name = full_name if not full_name: self.full_name = test.full_name self.test = test self.context = context_factory(self) self.description = description self.test_suites = registry.AutoKeyRegistry(lambda suite: suite.name)