blob: d331e01bf0c821c3f74d0d0c8aac384358d1e367 [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
ade@google.com6e54a912010-12-10 22:26:04 +00009# Conditional import of cleanup function
10try:
11 from tests.utils import cleanup
12except:
13 def cleanup():
14 pass
15
16# Ensure current working directory is in path
Joe Gregorio48d361f2010-08-18 13:19:21 -040017sys.path.insert(0, os.getcwd())
18
ade@google.com6e54a912010-12-10 22:26:04 +000019def build_suite(folder, verbosity):
ade@google.com46179d32010-08-21 00:00:03 +010020 # find all of the test modules
ade@google.com5ff125d2010-12-10 22:54:02 +000021 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 Gregoriodb849af2010-09-22 16:53:59 -040025 if verbosity > 0:
26 print "Running the tests found in the following modules:"
27 print modules
Joe Gregorio48d361f2010-08-18 13:19:21 -040028
ade@google.com46179d32010-08-21 00:00:03 +010029 # load all of the tests into a suite
30 try:
ade@google.com6e54a912010-12-10 22:26:04 +000031 return unittest.TestLoader().loadTestsFromNames(modules)
ade@google.com46179d32010-08-21 00:00:03 +010032 except Exception, exception:
ade@google.com6e54a912010-12-10 22:26:04 +000033 # attempt to produce a more specific message
34 for module in modules:
35 __import__(module)
36 raise
Joe Gregorio48d361f2010-08-18 13:19:21 -040037
Joe Gregorio93841702011-03-02 16:12:05 -080038def run(test_folder_name, verbosity, exit_on_failure):
ade@google.com6e54a912010-12-10 22:26:04 +000039 # Build and run the tests in test_folder_name
40 tests = build_suite(test_folder_name, verbosity)
Joe Gregorio93841702011-03-02 16:12:05 -080041 result = unittest.TextTestRunner(verbosity=verbosity).run(tests)
42 if exit_on_failure and not result.wasSuccessful():
43 sys.exit(1)
ade@google.com6e54a912010-12-10 22:26:04 +000044 cleanup()
ade@google.com50dce602010-09-22 17:42:53 +010045
ade@google.com6e54a912010-12-10 22:26:04 +000046def main():
ade@google.com5d716f62011-01-03 17:58:44 +000047 if '--help' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080048 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 +000049 return
ade@google.com6e54a912010-12-10 22:26:04 +000050 verbosity = 1
Joe Gregorio93841702011-03-02 16:12:05 -080051 exit_on_failure = '--exit_on_failure' in sys.argv
ade@google.com5d716f62011-01-03 17:58:44 +000052 if '-q' in sys.argv or '--quiet' in sys.argv:
ade@google.com6e54a912010-12-10 22:26:04 +000053 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 Gregoriob843fa22010-12-13 16:26:07 -050062
ade@google.com0cb157a2010-12-10 22:32:36 +000063 # Allow user to run a specific folder of tests
64 if 'tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080065 run('tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000066 elif 'functional_tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080067 run('functional_tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000068 elif 'contrib_tests' in sys.argv:
Joe Gregorio93841702011-03-02 16:12:05 -080069 run('contrib_tests', verbosity, exit_on_failure)
ade@google.com0cb157a2010-12-10 22:32:36 +000070 else:
Joe Gregorio93841702011-03-02 16:12:05 -080071 run('tests', verbosity, exit_on_failure)
72 run('functional_tests', verbosity, exit_on_failure)
73 run('contrib_tests', verbosity, exit_on_failure)
ade@google.com6e54a912010-12-10 22:26:04 +000074
75if __name__ == '__main__':
Joe Gregoriob843fa22010-12-13 16:26:07 -050076 main()