Chromium Code Reviews| Index: install_test/install_test.py |
| =================================================================== |
| --- install_test/install_test.py (revision 163625) |
| +++ install_test/install_test.py (working copy) |
| @@ -11,6 +11,7 @@ |
| """ |
| import atexit |
| +import fnmatch |
| import logging |
| import optparse |
| import os |
| @@ -250,6 +251,9 @@ |
| parser.add_option( |
| '--install-type', type='string', default='user', dest='install_type', |
| help='Type of installation (i.e., user, system, or both)') |
| + parser.add_option( |
| + '-f', '--filter', type='string', default=None, dest='filter', |
| + help='Filter that specifies the test or testsuite to run.') |
| self._opts, self._args = parser.parse_args() |
| self._ValidateArgs() |
| if(self._opts.install_type == 'system' or |
| @@ -262,10 +266,11 @@ |
| update_builds = (self._opts.update_builds.split(',') if |
| self._opts.update_builds else []) |
| options = self._opts.options.split(',') if self._opts.options else [] |
| - InstallTest.InitTestFixture(self._opts.install_build, |
| - update_builds, |
| - self._opts.url, |
| - options) |
| + InstallTest.InitTestFixture(self._opts.install_build, update_builds, |
| + self._opts.url, options) |
| + self._mod_name = os.path.splitext(os.path.basename(sys.argv[0]))[0] |
| + if os.path.dirname(sys.argv[0]) not in sys.path: |
|
kkania
2012/10/24 15:19:54
I think this is unnecessary. sys.argv[0] was just
nkang
2012/10/24 23:35:14
I agree. I was thinking the same thing that it sho
|
| + sys.path.append(os.path.dirname(sys.argv[0])) |
| def _ValidateArgs(self): |
| """Verifies the sanity of the command arguments. |
| @@ -290,16 +295,99 @@ |
| log_format = '%(asctime)s %(levelname)-8s %(message)s' |
| logging.basicConfig(level=logging.INFO, format=log_format) |
| - def _GetTests(self): |
| - """Returns a list of unittests from the calling script.""" |
| - mod_name = [os.path.splitext(os.path.basename(sys.argv[0]))[0]] |
| - if os.path.dirname(sys.argv[0]) not in sys.path: |
| - sys.path.append(os.path.dirname(sys.argv[0])) |
| - return unittest.defaultTestLoader.loadTestsFromNames(mod_name) |
| + def _GetFilteredTests(self): |
|
kkania
2012/10/24 15:19:54
move this whole Main class out to a new file, call
nkang
2012/10/24 23:35:14
Moved Main to a new file called run_install_tests.
|
| + """Returns a list of filtered unittests from the calling script.""" |
| + all_tests = unittest.defaultTestLoader.loadTestsFromName(self._mod_name) |
| + return self._FilterTestSuite(all_tests, self._opts.filter) |
| + def _GetTestsFromSuite(self, suite): |
| + """Returns all the tests from a given test suite. |
| + |
| + Args: |
| + suite: A unittest.TestSuite object. |
| + |
| + Returns: |
| + A list that contains all the tests found in the suite. |
| + """ |
| + tests = [] |
| + for test in suite: |
| + if isinstance(test, unittest.TestSuite): |
| + tests += self._GetTestsFromSuite(test) |
| + else: |
| + tests += [test] |
| + return tests |
| + |
| + def _GetTestName(self, test): |
| + """Gets the test name of the given unittest test. |
| + |
| + Args: |
| + test: A unittest test. |
| + |
| + Returns: |
| + A string representing the full test name. |
| + """ |
| + return '.'.join([test.__module__, test.__class__.__name__, |
| + test._testMethodName]) |
| + |
| + def _FilterTestSuite(self, suite, gtest_filter): |
| + """Returns a new filtered test suite based on the given gtest filter. |
| + |
| + See http://code.google.com/p/googletest/wiki/AdvancedGuide for |
| + gtest_filter specification. |
| + |
| + Args: |
| + suite: A unittest.TestSuite object, which can be obtained by calling |
| + |unittest.defaultTestLoader.loadTestsFromName|. |
| + gtest_filter: The gtest filter to use. Filter can be passed as follows: |
| + --filter=*className* or --filter=*testcaseName. |
| + |
| + Returns: |
| + A unittest.TestSuite object that contains tests that match the gtest |
| + filter. |
| + """ |
| + return unittest.TestSuite( |
| + self._FilterTests(self._GetTestsFromSuite(suite), gtest_filter)) |
| + |
| + def _FilterTests(self, all_tests, gtest_filter): |
| + """Returns a filtered list of tests based on the given gtest filter. |
| + |
| + Args: |
| + all_tests: A list that contains all unittests in a given suite. This |
| + list must be obtained by calling |_GetTestsFromSuite|. |
| + gtest_filter: The gtest filter to use. Filter can be passed as follows: |
| + *className* or *testcaseName. |
| + |
| + Returns: |
| + A list that contains all tests that match the given gtest filter. |
| + """ |
| + pattern_groups = gtest_filter.split('-') |
| + positive_patterns = pattern_groups[0].split(':') |
| + negative_patterns = None |
| + if len(pattern_groups) > 1: |
| + negative_patterns = pattern_groups[1].split(':') |
| + tests = [] |
| + for test in all_tests: |
| + test_name = self._GetTestName(test) |
| + # Test name must by matched by one positive pattern. |
| + for pattern in positive_patterns: |
| + if fnmatch.fnmatch(test_name, pattern): |
| + break |
| + else: |
| + continue |
| + # Test name must not be matched by any negative patterns. |
| + for pattern in negative_patterns or []: |
| + if fnmatch.fnmatch(test_name, pattern): |
| + break |
| + else: |
| + tests += [test] |
| + return tests |
| + |
| def _Run(self): |
| """Runs the unit tests.""" |
| - tests = self._GetTests() |
| + if self._opts.filter: |
| + tests = self._GetFilteredTests() |
| + else: |
| + tests = unittest.defaultTestLoader.loadTestsFromName(self._mod_name) |
| result = pyauto_utils.GTestTextTestRunner(verbosity=1).run(tests) |
| del(tests) |
| if not result.wasSuccessful(): |