OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Utilities for dealing with the python unittest module.""" | 5 """Utilities for dealing with the python unittest module.""" |
6 | 6 |
7 import fnmatch | 7 import fnmatch |
8 import sys | 8 import sys |
9 import unittest | 9 import unittest |
10 | 10 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 def GetTestName(test): | 87 def GetTestName(test): |
88 """Gets the test name of the given unittest test.""" | 88 """Gets the test name of the given unittest test.""" |
89 return '.'.join([test.__class__.__module__, | 89 return '.'.join([test.__class__.__module__, |
90 test.__class__.__name__, | 90 test.__class__.__name__, |
91 test._testMethodName]) | 91 test._testMethodName]) |
92 | 92 |
93 | 93 |
94 def FilterTestSuite(suite, gtest_filter): | 94 def FilterTestSuite(suite, gtest_filter): |
95 """Returns a new filtered tests suite based on the given gtest filter. | 95 """Returns a new filtered tests suite based on the given gtest filter. |
96 | 96 |
97 See http://code.google.com/p/googletest/wiki/AdvancedGuide | 97 See https://github.com/google/googletest/blob/master/googletest/docs/AdvancedG
uide.md |
98 for gtest_filter specification. | 98 for gtest_filter specification. |
99 """ | 99 """ |
100 return unittest.TestSuite(FilterTests(GetTestsFromSuite(suite), gtest_filter)) | 100 return unittest.TestSuite(FilterTests(GetTestsFromSuite(suite), gtest_filter)) |
101 | 101 |
102 | 102 |
103 def FilterTests(all_tests, gtest_filter): | 103 def FilterTests(all_tests, gtest_filter): |
104 """Filter a list of tests based on the given gtest filter. | 104 """Filter a list of tests based on the given gtest filter. |
105 | 105 |
106 Args: | 106 Args: |
107 all_tests: List of tests (unittest.TestSuite) | 107 all_tests: List of tests (unittest.TestSuite) |
108 gtest_filter: Filter to apply. | 108 gtest_filter: Filter to apply. |
109 | 109 |
110 Returns: | 110 Returns: |
111 Filtered subset of the given list of tests. | 111 Filtered subset of the given list of tests. |
112 """ | 112 """ |
113 test_names = [GetTestName(test) for test in all_tests] | 113 test_names = [GetTestName(test) for test in all_tests] |
114 filtered_names = FilterTestNames(test_names, gtest_filter) | 114 filtered_names = FilterTestNames(test_names, gtest_filter) |
115 return [test for test in all_tests if GetTestName(test) in filtered_names] | 115 return [test for test in all_tests if GetTestName(test) in filtered_names] |
116 | 116 |
117 | 117 |
118 def FilterTestNames(all_tests, gtest_filter): | 118 def FilterTestNames(all_tests, gtest_filter): |
119 """Filter a list of test names based on the given gtest filter. | 119 """Filter a list of test names based on the given gtest filter. |
120 | 120 |
121 See http://code.google.com/p/googletest/wiki/AdvancedGuide | 121 See https://github.com/google/googletest/blob/master/googletest/docs/AdvancedG
uide.md |
122 for gtest_filter specification. | 122 for gtest_filter specification. |
123 | 123 |
124 Args: | 124 Args: |
125 all_tests: List of test names. | 125 all_tests: List of test names. |
126 gtest_filter: Filter to apply. | 126 gtest_filter: Filter to apply. |
127 | 127 |
128 Returns: | 128 Returns: |
129 Filtered subset of the given list of test names. | 129 Filtered subset of the given list of test names. |
130 """ | 130 """ |
131 pattern_groups = gtest_filter.split('-') | 131 pattern_groups = gtest_filter.split('-') |
132 positive_patterns = ['*'] | 132 positive_patterns = ['*'] |
133 if pattern_groups[0]: | 133 if pattern_groups[0]: |
134 positive_patterns = pattern_groups[0].split(':') | 134 positive_patterns = pattern_groups[0].split(':') |
135 negative_patterns = [] | 135 negative_patterns = [] |
136 if len(pattern_groups) > 1: | 136 if len(pattern_groups) > 1: |
137 negative_patterns = pattern_groups[1].split(':') | 137 negative_patterns = pattern_groups[1].split(':') |
138 | 138 |
139 tests = [] | 139 tests = [] |
140 test_set = set() | 140 test_set = set() |
141 for pattern in positive_patterns: | 141 for pattern in positive_patterns: |
142 pattern_tests = [ | 142 pattern_tests = [ |
143 test for test in all_tests | 143 test for test in all_tests |
144 if (fnmatch.fnmatch(test, pattern) | 144 if (fnmatch.fnmatch(test, pattern) |
145 and not any(fnmatch.fnmatch(test, p) for p in negative_patterns) | 145 and not any(fnmatch.fnmatch(test, p) for p in negative_patterns) |
146 and test not in test_set)] | 146 and test not in test_set)] |
147 tests.extend(pattern_tests) | 147 tests.extend(pattern_tests) |
148 test_set.update(pattern_tests) | 148 test_set.update(pattern_tests) |
149 return tests | 149 return tests |
OLD | NEW |