3 # This script uses the unittest module to find all the tests in the
4 # same directory and run them.
6 # 2009-01-23, Roman Stanchak (rstanchak@gmail.com)
9 # For a test to be detected and run by this script, it must
11 # 2. define a suite() method that returns a unittest.TestSuite containing
20 def suites( dirname ):
23 for fn in os.listdir( dirname ):
24 # tests must be named test_*.py or *_tests.py
25 if not ( fn.lower().endswith('.py') and
26 (fn.lower().startswith('test_') or fn.lower().endswith('_tests.py')) ):
29 module_name = fn[0:-3]
30 fullpath = os.path.realpath( dirname + os.path.sep + fn )
33 test_module = imp.load_source( module_name, fullpath )
35 print "Error importing python code in '%s'" % fn
38 suite_list.append( test_module.suite() )
39 print "Added tests from %s" % fn
41 print "%s does not contain a suite() method, skipping" % fn
42 return unittest.TestSuite(suite_list)
45 def col2( c1, c2, w=72 ):
46 return "%s%s" % (c1, c2.rjust(w-len(c1)))
48 if __name__ == "__main__":
49 print '----------------------------------------------------------------------'
50 print 'Searching for tests...'
51 print '----------------------------------------------------------------------'
52 suite = suites( os.path.dirname( os.path.realpath(__file__) ))
53 print '----------------------------------------------------------------------'
54 print 'Running tests...'
55 print '----------------------------------------------------------------------'
56 unittest.TextTestRunner(verbosity=2).run(suite)