Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 2 | import glob |
| 3 | import logging |
| 4 | import os |
| 5 | import sys |
| 6 | import unittest |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 7 | from trace import fullmodname |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 8 | |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 9 | # Conditional import of cleanup function |
| 10 | try: |
| 11 | from tests.utils import cleanup |
| 12 | except: |
| 13 | def cleanup(): |
| 14 | pass |
| 15 | |
| 16 | # Ensure current working directory is in path |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 17 | sys.path.insert(0, os.getcwd()) |
| 18 | |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 19 | def build_suite(folder, verbosity): |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 20 | # find all of the test modules |
ade@google.com | 5ff125d | 2010-12-10 22:54:02 +0000 | [diff] [blame] | 21 | top_level_modules = map(fullmodname, glob.glob(os.path.join(folder, 'test_*.py'))) |
| 22 | # TODO(ade) Verify that this works on Windows. If it doesn't then switch to os.walk instead |
| 23 | lower_level_modules = map(fullmodname, glob.glob(os.path.join(folder, '*/test_*.py'))) |
| 24 | modules = top_level_modules + lower_level_modules |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 25 | if verbosity > 0: |
| 26 | print "Running the tests found in the following modules:" |
| 27 | print modules |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 28 | |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 29 | # load all of the tests into a suite |
| 30 | try: |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 31 | return unittest.TestLoader().loadTestsFromNames(modules) |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 32 | except Exception, exception: |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 33 | # attempt to produce a more specific message |
| 34 | for module in modules: |
| 35 | __import__(module) |
| 36 | raise |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 37 | |
Joe Gregorio | 9384170 | 2011-03-02 16:12:05 -0800 | [diff] [blame] | 38 | def run(test_folder_name, verbosity, exit_on_failure): |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 39 | # Build and run the tests in test_folder_name |
| 40 | tests = build_suite(test_folder_name, verbosity) |
Joe Gregorio | 9384170 | 2011-03-02 16:12:05 -0800 | [diff] [blame] | 41 | result = unittest.TextTestRunner(verbosity=verbosity).run(tests) |
| 42 | if exit_on_failure and not result.wasSuccessful(): |
| 43 | sys.exit(1) |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 44 | cleanup() |
ade@google.com | 50dce60 | 2010-09-22 17:42:53 +0100 | [diff] [blame] | 45 | |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 46 | def main(): |
ade@google.com | 5d716f6 | 2011-01-03 17:58:44 +0000 | [diff] [blame] | 47 | if '--help' in sys.argv: |
Joe Gregorio | 9384170 | 2011-03-02 16:12:05 -0800 | [diff] [blame] | 48 | print 'Usage: python runtests.py [-q|--quiet|-v|--verbose] [--exit_on_failure] [tests|functional_tests|contrib_tests]' |
ade@google.com | 5d716f6 | 2011-01-03 17:58:44 +0000 | [diff] [blame] | 49 | return |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 50 | verbosity = 1 |
Joe Gregorio | 9384170 | 2011-03-02 16:12:05 -0800 | [diff] [blame] | 51 | exit_on_failure = '--exit_on_failure' in sys.argv |
ade@google.com | 5d716f6 | 2011-01-03 17:58:44 +0000 | [diff] [blame] | 52 | if '-q' in sys.argv or '--quiet' in sys.argv: |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 53 | verbosity = 0 |
| 54 | if "-v" in sys.argv or '--verbose' in sys.argv: |
| 55 | verbosity = 2 |
| 56 | if verbosity == 0: |
| 57 | logging.disable(logging.CRITICAL) |
| 58 | elif verbosity == 1: |
| 59 | logging.disable(logging.ERROR) |
| 60 | elif verbosity == 2: |
| 61 | logging.basicConfig(level=logging.DEBUG) |
Joe Gregorio | b843fa2 | 2010-12-13 16:26:07 -0500 | [diff] [blame] | 62 | |
ade@google.com | 0cb157a | 2010-12-10 22:32:36 +0000 | [diff] [blame] | 63 | # Allow user to run a specific folder of tests |
| 64 | if 'tests' in sys.argv: |
Joe Gregorio | 9384170 | 2011-03-02 16:12:05 -0800 | [diff] [blame] | 65 | run('tests', verbosity, exit_on_failure) |
ade@google.com | 0cb157a | 2010-12-10 22:32:36 +0000 | [diff] [blame] | 66 | elif 'functional_tests' in sys.argv: |
Joe Gregorio | 9384170 | 2011-03-02 16:12:05 -0800 | [diff] [blame] | 67 | run('functional_tests', verbosity, exit_on_failure) |
ade@google.com | 0cb157a | 2010-12-10 22:32:36 +0000 | [diff] [blame] | 68 | elif 'contrib_tests' in sys.argv: |
Joe Gregorio | 9384170 | 2011-03-02 16:12:05 -0800 | [diff] [blame] | 69 | run('contrib_tests', verbosity, exit_on_failure) |
ade@google.com | 0cb157a | 2010-12-10 22:32:36 +0000 | [diff] [blame] | 70 | else: |
Joe Gregorio | 9384170 | 2011-03-02 16:12:05 -0800 | [diff] [blame] | 71 | run('tests', verbosity, exit_on_failure) |
| 72 | run('functional_tests', verbosity, exit_on_failure) |
| 73 | run('contrib_tests', verbosity, exit_on_failure) |
ade@google.com | 6e54a91 | 2010-12-10 22:26:04 +0000 | [diff] [blame] | 74 | |
| 75 | if __name__ == '__main__': |
Joe Gregorio | b843fa2 | 2010-12-13 16:26:07 -0500 | [diff] [blame] | 76 | main() |