blob: ecd53b1cb1281c6ae5b5327ebdafd375d66632e3 [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():
45 verbosity = 1
46 if "-q" in sys.argv or '--quiet' in sys.argv:
47 verbosity = 0
48 if "-v" in sys.argv or '--verbose' in sys.argv:
49 verbosity = 2
50 if verbosity == 0:
51 logging.disable(logging.CRITICAL)
52 elif verbosity == 1:
53 logging.disable(logging.ERROR)
54 elif verbosity == 2:
55 logging.basicConfig(level=logging.DEBUG)
Joe Gregoriob843fa22010-12-13 16:26:07 -050056
ade@google.com0cb157a2010-12-10 22:32:36 +000057 # Allow user to run a specific folder of tests
58 if 'tests' in sys.argv:
59 run('tests', verbosity)
60 elif 'functional_tests' in sys.argv:
61 run('functional_tests', verbosity)
62 elif 'contrib_tests' in sys.argv:
63 run('contrib_tests', verbosity)
64 else:
65 run('tests', verbosity)
66 run('functional_tests', verbosity)
67 run('contrib_tests', verbosity)
ade@google.com6e54a912010-12-10 22:26:04 +000068
69if __name__ == '__main__':
Joe Gregoriob843fa22010-12-13 16:26:07 -050070 main()