| 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 """Returns a filtered list of tests based on the given gtest filter. | 104 """Returns a filtered list of tests based on the given gtest filter. |
| 105 | 105 |
| 106 See http://code.google.com/p/googletest/wiki/AdvancedGuide | 106 See https://github.com/google/googletest/blob/master/googletest/docs/AdvancedG
uide.md |
| 107 for gtest_filter specification. | 107 for gtest_filter specification. |
| 108 """ | 108 """ |
| 109 pattern_groups = gtest_filter.split('-') | 109 pattern_groups = gtest_filter.split('-') |
| 110 positive_patterns = pattern_groups[0].split(':') | 110 positive_patterns = pattern_groups[0].split(':') |
| 111 negative_patterns = None | 111 negative_patterns = None |
| 112 if len(pattern_groups) > 1: | 112 if len(pattern_groups) > 1: |
| 113 negative_patterns = pattern_groups[1].split(':') | 113 negative_patterns = pattern_groups[1].split(':') |
| 114 | 114 |
| 115 tests = [] | 115 tests = [] |
| 116 for test in all_tests: | 116 for test in all_tests: |
| 117 test_name = GetTestName(test) | 117 test_name = GetTestName(test) |
| 118 # Test name must by matched by one positive pattern. | 118 # Test name must by matched by one positive pattern. |
| 119 for pattern in positive_patterns: | 119 for pattern in positive_patterns: |
| 120 if fnmatch.fnmatch(test_name, pattern): | 120 if fnmatch.fnmatch(test_name, pattern): |
| 121 break | 121 break |
| 122 else: | 122 else: |
| 123 continue | 123 continue |
| 124 # Test name must not be matched by any negative patterns. | 124 # Test name must not be matched by any negative patterns. |
| 125 for pattern in negative_patterns or []: | 125 for pattern in negative_patterns or []: |
| 126 if fnmatch.fnmatch(test_name, pattern): | 126 if fnmatch.fnmatch(test_name, pattern): |
| 127 break | 127 break |
| 128 else: | 128 else: |
| 129 tests += [test] | 129 tests += [test] |
| 130 return tests | 130 return tests |
| OLD | NEW |