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