| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Module containing information about the host-driven tests.""" | |
| 6 | |
| 7 import logging | |
| 8 import os | |
| 9 | |
| 10 from pylib.constants import host_paths | |
| 11 from pylib.host_driven import tests_annotations | |
| 12 | |
| 13 with host_paths.SysPath(host_paths.BUILD_COMMON_PATH): | |
| 14 import unittest_util # pylint: disable=import-error | |
| 15 | |
| 16 class TestInfo(object): | |
| 17 """An object containing and representing a test function, plus metadata.""" | |
| 18 | |
| 19 def __init__(self, runnable, set_up=None, tear_down=None): | |
| 20 # The actual test function/method. | |
| 21 self.runnable = runnable | |
| 22 # Qualified name of test function/method (e.g. FooModule.testBar). | |
| 23 self.qualified_name = self._GetQualifiedName(runnable) | |
| 24 # setUp and teardown functions, if any. | |
| 25 self.set_up = set_up | |
| 26 self.tear_down = tear_down | |
| 27 | |
| 28 @staticmethod | |
| 29 def _GetQualifiedName(runnable): | |
| 30 """Helper method to infer a runnable's name and module name. | |
| 31 | |
| 32 Many filters and lists presuppose a format of module_name.testMethodName. | |
| 33 To make this easy on everyone, we use some reflection magic to infer this | |
| 34 name automatically. | |
| 35 | |
| 36 Args: | |
| 37 runnable: the test method to get the qualified name for | |
| 38 | |
| 39 Returns: | |
| 40 qualified name for this runnable, incl. module name and method name. | |
| 41 """ | |
| 42 runnable_name = runnable.__name__ | |
| 43 # See also tests_annotations. | |
| 44 module_name = os.path.splitext( | |
| 45 os.path.basename(runnable.__globals__['__file__']))[0] | |
| 46 return '.'.join([module_name, runnable_name]) | |
| 47 | |
| 48 def __str__(self): | |
| 49 return self.qualified_name | |
| 50 | |
| 51 | |
| 52 class TestInfoCollection(object): | |
| 53 """A collection of TestInfo objects which facilitates filtering.""" | |
| 54 | |
| 55 def __init__(self): | |
| 56 """Initialize a new TestInfoCollection.""" | |
| 57 # Master list of all valid tests. | |
| 58 self.all_tests = [] | |
| 59 | |
| 60 def AddTests(self, test_infos): | |
| 61 """Adds a set of tests to this collection. | |
| 62 | |
| 63 The user may then retrieve them, optionally according to criteria, via | |
| 64 GetAvailableTests(). | |
| 65 | |
| 66 Args: | |
| 67 test_infos: a list of TestInfos representing test functions/methods. | |
| 68 """ | |
| 69 self.all_tests = test_infos | |
| 70 | |
| 71 def GetAvailableTests(self, annotations, exclude_annotations, name_filter): | |
| 72 """Get a collection of TestInfos which match the supplied criteria. | |
| 73 | |
| 74 Args: | |
| 75 annotations: List of annotations. Each test in the returned list is | |
| 76 annotated with atleast one of these annotations. | |
| 77 exclude_annotations: List of annotations. The tests in the returned | |
| 78 list are not annotated with any of these annotations. | |
| 79 name_filter: name filter which tests must match, if any | |
| 80 | |
| 81 Returns: | |
| 82 List of available tests. | |
| 83 """ | |
| 84 available_tests = self.all_tests | |
| 85 | |
| 86 # Filter out tests which match neither the requested annotation, nor the | |
| 87 # requested name filter, if any. | |
| 88 available_tests = [t for t in available_tests if | |
| 89 self._AnnotationIncludesTest(t, annotations)] | |
| 90 if annotations and len(annotations) == 1 and annotations[0] == 'SmallTest': | |
| 91 tests_without_annotation = [ | |
| 92 t for t in self.all_tests if | |
| 93 not tests_annotations.AnnotatedFunctions.GetTestAnnotations( | |
| 94 t.qualified_name)] | |
| 95 test_names = [t.qualified_name for t in tests_without_annotation] | |
| 96 logging.warning('The following tests do not contain any annotation. ' | |
| 97 'Assuming "SmallTest":\n%s', | |
| 98 '\n'.join(test_names)) | |
| 99 available_tests += tests_without_annotation | |
| 100 if exclude_annotations: | |
| 101 excluded_tests = [t for t in available_tests if | |
| 102 self._AnnotationIncludesTest(t, exclude_annotations)] | |
| 103 available_tests = list(set(available_tests) - set(excluded_tests)) | |
| 104 | |
| 105 if name_filter: | |
| 106 available_test_names = unittest_util.FilterTestNames( | |
| 107 [t.qualified_name for t in available_tests], name_filter) | |
| 108 available_tests = [ | |
| 109 t for t in available_tests if | |
| 110 t.qualified_name in available_test_names] | |
| 111 return available_tests | |
| 112 | |
| 113 @staticmethod | |
| 114 def _AnnotationIncludesTest(test_info, annotation_filter_list): | |
| 115 """Checks whether a given test represented by test_info matches annotation. | |
| 116 | |
| 117 Args: | |
| 118 test_info: TestInfo object representing the test | |
| 119 annotation_filter_list: list of annotation filters to match (e.g. Smoke) | |
| 120 | |
| 121 Returns: | |
| 122 True if no annotation was supplied or the test matches; false otherwise. | |
| 123 """ | |
| 124 if not annotation_filter_list: | |
| 125 return True | |
| 126 for annotation_filter in annotation_filter_list: | |
| 127 filters = annotation_filter.split('=') | |
| 128 if len(filters) == 2: | |
| 129 key = filters[0] | |
| 130 value_list = filters[1].split(',') | |
| 131 for value in value_list: | |
| 132 if tests_annotations.AnnotatedFunctions.IsAnnotated( | |
| 133 key + ':' + value, test_info.qualified_name): | |
| 134 return True | |
| 135 elif tests_annotations.AnnotatedFunctions.IsAnnotated( | |
| 136 annotation_filter, test_info.qualified_name): | |
| 137 return True | |
| 138 return False | |
| 139 | |
| OLD | NEW |