blob: 6fd426df60f00fec0a9e013248d1ed9dd9c52cef [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
ade@google.com6e54a912010-12-10 22:26:04 +000038def run(test_folder_name, verbosity=1):
39 # Build and run the tests in test_folder_name
40 tests = build_suite(test_folder_name, verbosity)
41 unittest.TextTestRunner(verbosity=verbosity).run(tests)
42 cleanup()
ade@google.com50dce602010-09-22 17:42:53 +010043
ade@google.com6e54a912010-12-10 22:26:04 +000044def main():
ade@google.com5d716f62011-01-03 17:58:44 +000045 if '--help' in sys.argv:
46 print 'Usage: python runtests.py [--q|-quiet|--v|-verbose] [tests|functional_tests|contrib_tests]'
47 return
ade@google.com6e54a912010-12-10 22:26:04 +000048 verbosity = 1
ade@google.com5d716f62011-01-03 17:58:44 +000049 if '-q' in sys.argv or '--quiet' in sys.argv:
ade@google.com6e54a912010-12-10 22:26:04 +000050 verbosity = 0
51 if "-v" in sys.argv or '--verbose' in sys.argv:
52 verbosity = 2
53 if verbosity == 0:
54 logging.disable(logging.CRITICAL)
55 elif verbosity == 1:
56 logging.disable(logging.ERROR)
57 elif verbosity == 2:
58 logging.basicConfig(level=logging.DEBUG)
Joe Gregoriob843fa22010-12-13 16:26:07 -050059
ade@google.com0cb157a2010-12-10 22:32:36 +000060 # Allow user to run a specific folder of tests
61 if 'tests' in sys.argv:
62 run('tests', verbosity)
63 elif 'functional_tests' in sys.argv:
64 run('functional_tests', verbosity)
65 elif 'contrib_tests' in sys.argv:
66 run('contrib_tests', verbosity)
67 else:
68 run('tests', verbosity)
69 run('functional_tests', verbosity)
70 run('contrib_tests', verbosity)
ade@google.com6e54a912010-12-10 22:26:04 +000071
72if __name__ == '__main__':
Joe Gregoriob843fa22010-12-13 16:26:07 -050073 main()