blob: a09a6c078d25edddc5a904612a607dd90e90bf20 [file] [log] [blame]
Joe Gregorio48d361f2010-08-18 13:19:21 -04001#!/usr/bin/env python
Joe Gregoriofe695fb2010-08-30 12:04:04 -04002import glob
3import logging
4import os
5import sys
6import unittest
Joe Gregorio48d361f2010-08-18 13:19:21 -04007from trace import fullmodname
Joe Gregorio48d361f2010-08-18 13:19:21 -04008
Joe Gregorio432f17e2011-05-22 23:18:00 -04009APP_ENGINE_PATH='../google_appengine'
10
ade@google.com6e54a912010-12-10 22:26:04 +000011# Conditional import of cleanup function
12try:
13 from tests.utils import cleanup
14except:
15 def cleanup():
16 pass
17
18# Ensure current working directory is in path
Joe Gregorio48d361f2010-08-18 13:19:21 -040019sys.path.insert(0, os.getcwd())
Joe Gregorio432f17e2011-05-22 23:18:00 -040020sys.path.insert(0, APP_ENGINE_PATH)
Joe Gregorio48d361f2010-08-18 13:19:21 -040021
ade@google.com6e54a912010-12-10 22:26:04 +000022def build_suite(folder, verbosity):
ade@google.com46179d32010-08-21 00:00:03 +010023 # find all of the test modules
ade@google.com5ff125d2010-12-10 22:54:02 +000024 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 Gregoriodb849af2010-09-22 16:53:59 -040028 if verbosity > 0:
29 print "Running the tests found in the following modules:"
30 print modules
Joe Gregorio48d361f2010-08-18 13:19:21 -040031
ade@google.com46179d32010-08-21 00:00:03 +010032 # load all of the tests into a suite
33 try:
ade@google.com6e54a912010-12-10 22:26:04 +000034 return unittest.TestLoader().loadTestsFromNames(modules)
ade@google.com46179d32010-08-21 00:00:03 +010035 except Exception, exception:
ade@google.com6e54a912010-12-10 22:26:04 +000036 # attempt to produce a more specific message
37 for module in modules:
38 __import__(module)
39 raise
Joe Gregorio48d361f2010-08-18 13:19:21 -040040
Joe Gregorio93841702011-03-02 16:12:05 -080041def run(test_folder_name, verbosity, exit_on_failure):
ade@google.com6e54a912010-12-10 22:26:04 +000042 # Build and run the tests in test_folder_name
43 tests = build_suite(test_folder_name, verbosity)
Joe Gregorio93841702011-03-02 16:12:05 -080044 result = unittest.TextTestRunner(verbosity=verbosity).run(tests)
45 if exit_on_failure and not result.wasSuccessful():
46 sys.exit(1)
ade@google.com6e54a912010-12-10 22:26:04 +000047 cleanup()
ade@google.com50dce602010-09-22 17:42:53 +010048
ade@google.com6e54a912010-12-10 22:26:04 +000049def main():
ade@google.com5d716f62011-01-03 17:58:44 +000050 if '--help' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080051 print 'Usage: python runtests.py [-q|--quiet|-v|--verbose] [--exit_on_failure] [tests|functional_tests|contrib_tests]'
ade@google.com5d716f62011-01-03 17:58:44 +000052 return
ade@google.com6e54a912010-12-10 22:26:04 +000053 verbosity = 1
Joe Gregorio93841702011-03-02 16:12:05 -080054 exit_on_failure = '--exit_on_failure' in sys.argv
ade@google.com5d716f62011-01-03 17:58:44 +000055 if '-q' in sys.argv or '--quiet' in sys.argv:
ade@google.com6e54a912010-12-10 22:26:04 +000056 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 Gregoriob843fa22010-12-13 16:26:07 -050065
Joe Gregorio432f17e2011-05-22 23:18:00 -040066 import dev_appserver
67 dev_appserver.fix_sys_path()
ade@google.com0cb157a2010-12-10 22:32:36 +000068 # Allow user to run a specific folder of tests
69 if 'tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080070 run('tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000071 elif 'functional_tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080072 run('functional_tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000073 elif 'contrib_tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080074 run('contrib_tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000075 else:
Joe Gregorio93841702011-03-02 16:12:05 -080076 run('tests', verbosity, exit_on_failure)
77 run('functional_tests', verbosity, exit_on_failure)
78 run('contrib_tests', verbosity, exit_on_failure)
ade@google.com6e54a912010-12-10 22:26:04 +000079
80if __name__ == '__main__':
Joe Gregoriob843fa22010-12-13 16:26:07 -050081 main()