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