michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: """Module containing information about the python-driven tests.""" michael@0: michael@0: import logging michael@0: import os michael@0: michael@0: import tests_annotations michael@0: michael@0: michael@0: class TestInfo(object): michael@0: """An object containing and representing a test function, plus metadata.""" michael@0: michael@0: def __init__(self, runnable, set_up=None, tear_down=None): michael@0: # The actual test function/method. michael@0: self.runnable = runnable michael@0: # Qualified name of test function/method (e.g. FooModule.testBar). michael@0: self.qualified_name = self._GetQualifiedName(runnable) michael@0: # setUp and teardown functions, if any. michael@0: self.set_up = set_up michael@0: self.tear_down = tear_down michael@0: michael@0: def _GetQualifiedName(self, runnable): michael@0: """Helper method to infer a runnable's name and module name. michael@0: michael@0: Many filters and lists presuppose a format of module_name.testMethodName. michael@0: To make this easy on everyone, we use some reflection magic to infer this michael@0: name automatically. michael@0: michael@0: Args: michael@0: runnable: the test method to get the qualified name for michael@0: michael@0: Returns: michael@0: qualified name for this runnable, incl. module name and method name. michael@0: """ michael@0: runnable_name = runnable.__name__ michael@0: # See also tests_annotations. michael@0: module_name = os.path.splitext( michael@0: os.path.basename(runnable.__globals__['__file__']))[0] michael@0: return '.'.join([module_name, runnable_name]) michael@0: michael@0: def __str__(self): michael@0: return self.qualified_name michael@0: michael@0: michael@0: class TestInfoCollection(object): michael@0: """A collection of TestInfo objects which facilitates filtering.""" michael@0: michael@0: def __init__(self): michael@0: """Initialize a new TestInfoCollection.""" michael@0: # Master list of all valid tests. michael@0: self.all_tests = [] michael@0: michael@0: def AddTests(self, test_infos): michael@0: """Adds a set of tests to this collection. michael@0: michael@0: The user may then retrieve them, optionally according to criteria, via michael@0: GetAvailableTests(). michael@0: michael@0: Args: michael@0: test_infos: a list of TestInfos representing test functions/methods. michael@0: """ michael@0: self.all_tests = test_infos michael@0: michael@0: def GetAvailableTests(self, annotation, name_filter): michael@0: """Get a collection of TestInfos which match the supplied criteria. michael@0: michael@0: Args: michael@0: annotation: annotation which tests must match, if any michael@0: name_filter: name filter which tests must match, if any michael@0: michael@0: Returns: michael@0: List of available tests. michael@0: """ michael@0: available_tests = self.all_tests michael@0: michael@0: # Filter out tests which match neither the requested annotation, nor the michael@0: # requested name filter, if any. michael@0: available_tests = [t for t in available_tests if michael@0: self._AnnotationIncludesTest(t, annotation)] michael@0: if annotation and len(annotation) == 1 and annotation[0] == 'SmallTest': michael@0: tests_without_annotation = [ michael@0: t for t in self.all_tests if michael@0: not tests_annotations.AnnotatedFunctions.GetTestAnnotations( michael@0: t.qualified_name)] michael@0: test_names = [t.qualified_name for t in tests_without_annotation] michael@0: logging.warning('The following tests do not contain any annotation. ' michael@0: 'Assuming "SmallTest":\n%s', michael@0: '\n'.join(test_names)) michael@0: available_tests += tests_without_annotation michael@0: available_tests = [t for t in available_tests if michael@0: self._NameFilterIncludesTest(t, name_filter)] michael@0: michael@0: return available_tests michael@0: michael@0: def _AnnotationIncludesTest(self, test_info, annotation_filter_list): michael@0: """Checks whether a given test represented by test_info matches annotation. michael@0: michael@0: Args: michael@0: test_info: TestInfo object representing the test michael@0: annotation_filter_list: list of annotation filters to match (e.g. Smoke) michael@0: michael@0: Returns: michael@0: True if no annotation was supplied or the test matches; false otherwise. michael@0: """ michael@0: if not annotation_filter_list: michael@0: return True michael@0: for annotation_filter in annotation_filter_list: michael@0: filters = annotation_filter.split('=') michael@0: if len(filters) == 2: michael@0: key = filters[0] michael@0: value_list = filters[1].split(',') michael@0: for value in value_list: michael@0: if tests_annotations.AnnotatedFunctions.IsAnnotated( michael@0: key + ':' + value, test_info.qualified_name): michael@0: return True michael@0: elif tests_annotations.AnnotatedFunctions.IsAnnotated( michael@0: annotation_filter, test_info.qualified_name): michael@0: return True michael@0: return False michael@0: michael@0: def _NameFilterIncludesTest(self, test_info, name_filter): michael@0: """Checks whether a name filter matches a given test_info's method name. michael@0: michael@0: This is a case-sensitive, substring comparison: 'Foo' will match methods michael@0: Foo.testBar and Bar.testFoo. 'foo' would not match either. michael@0: michael@0: Args: michael@0: test_info: TestInfo object representing the test michael@0: name_filter: substring to check for in the qualified name of the test michael@0: michael@0: Returns: michael@0: True if no name filter supplied or it matches; False otherwise. michael@0: """ michael@0: return not name_filter or name_filter in test_info.qualified_name