blob: c1f9d1d2e6bfda7fa2754fe212fd6e70ba6f5ad8 [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
Joe Gregoriof42dfdc2011-10-14 13:55:24 -040022from google.appengine.dist import use_library
23use_library('django', '1.2')
24
25
ade@google.com6e54a912010-12-10 22:26:04 +000026def build_suite(folder, verbosity):
ade@google.com46179d32010-08-21 00:00:03 +010027 # find all of the test modules
ade@google.com5ff125d2010-12-10 22:54:02 +000028 top_level_modules = map(fullmodname, glob.glob(os.path.join(folder, 'test_*.py')))
29 # TODO(ade) Verify that this works on Windows. If it doesn't then switch to os.walk instead
30 lower_level_modules = map(fullmodname, glob.glob(os.path.join(folder, '*/test_*.py')))
31 modules = top_level_modules + lower_level_modules
Joe Gregoriodb849af2010-09-22 16:53:59 -040032 if verbosity > 0:
33 print "Running the tests found in the following modules:"
34 print modules
Joe Gregorio48d361f2010-08-18 13:19:21 -040035
ade@google.com46179d32010-08-21 00:00:03 +010036 # load all of the tests into a suite
37 try:
ade@google.com6e54a912010-12-10 22:26:04 +000038 return unittest.TestLoader().loadTestsFromNames(modules)
ade@google.com46179d32010-08-21 00:00:03 +010039 except Exception, exception:
ade@google.com6e54a912010-12-10 22:26:04 +000040 # attempt to produce a more specific message
41 for module in modules:
42 __import__(module)
43 raise
Joe Gregorio48d361f2010-08-18 13:19:21 -040044
Joe Gregorio93841702011-03-02 16:12:05 -080045def run(test_folder_name, verbosity, exit_on_failure):
ade@google.com6e54a912010-12-10 22:26:04 +000046 # Build and run the tests in test_folder_name
47 tests = build_suite(test_folder_name, verbosity)
Joe Gregorio93841702011-03-02 16:12:05 -080048 result = unittest.TextTestRunner(verbosity=verbosity).run(tests)
49 if exit_on_failure and not result.wasSuccessful():
50 sys.exit(1)
ade@google.com6e54a912010-12-10 22:26:04 +000051 cleanup()
ade@google.com50dce602010-09-22 17:42:53 +010052
ade@google.com6e54a912010-12-10 22:26:04 +000053def main():
ade@google.com5d716f62011-01-03 17:58:44 +000054 if '--help' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080055 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 +000056 return
ade@google.com6e54a912010-12-10 22:26:04 +000057 verbosity = 1
Joe Gregorio93841702011-03-02 16:12:05 -080058 exit_on_failure = '--exit_on_failure' in sys.argv
ade@google.com5d716f62011-01-03 17:58:44 +000059 if '-q' in sys.argv or '--quiet' in sys.argv:
ade@google.com6e54a912010-12-10 22:26:04 +000060 verbosity = 0
61 if "-v" in sys.argv or '--verbose' in sys.argv:
62 verbosity = 2
63 if verbosity == 0:
64 logging.disable(logging.CRITICAL)
65 elif verbosity == 1:
66 logging.disable(logging.ERROR)
67 elif verbosity == 2:
68 logging.basicConfig(level=logging.DEBUG)
Joe Gregoriob843fa22010-12-13 16:26:07 -050069
Joe Gregorio432f17e2011-05-22 23:18:00 -040070 import dev_appserver
71 dev_appserver.fix_sys_path()
ade@google.com0cb157a2010-12-10 22:32:36 +000072 # Allow user to run a specific folder of tests
73 if 'tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080074 run('tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000075 elif 'functional_tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080076 run('functional_tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000077 elif 'contrib_tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080078 run('contrib_tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000079 else:
Joe Gregorio93841702011-03-02 16:12:05 -080080 run('tests', verbosity, exit_on_failure)
81 run('functional_tests', verbosity, exit_on_failure)
82 run('contrib_tests', verbosity, exit_on_failure)
ade@google.com6e54a912010-12-10 22:26:04 +000083
84if __name__ == '__main__':
Joe Gregoriob843fa22010-12-13 16:26:07 -050085 main()