Update to 2.0.0 tree from current Fremantle build
[opencv] / tests / swig_python / testall.py
1 #!/usr/bin/env python
2
3 # This script uses the unittest module to find all the tests in the 
4 # same directory and run them.
5 #
6 # 2009-01-23, Roman Stanchak (rstanchak@gmail.com)
7 #
8
9 # For a test to be detected and run by this script, it must
10 # 1. Use unittest
11 # 2. define a suite() method that returns a unittest.TestSuite containing
12 #    the tests to be run
13
14 import cvtestutils
15 import unittest
16 import types
17 import os
18 import imp
19
20 def suites( dirname ):
21     suite_list=[]
22
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')) ):
27             continue
28
29         module_name = fn[0:-3]
30         fullpath = os.path.realpath( dirname + os.path.sep + fn )
31         test_module = None
32         try:
33             test_module = imp.load_source( module_name, fullpath )
34         except:
35             print "Error importing python code in '%s'" % fn
36         if test_module:
37             try:
38                 suite_list.append( test_module.suite() )
39                 print "Added tests from %s" % fn
40             except:
41                 print "%s does not contain a suite() method, skipping" % fn
42     return unittest.TestSuite(suite_list)
43
44     
45 def col2( c1, c2, w=72 ):
46     return "%s%s" % (c1, c2.rjust(w-len(c1)))
47
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)