diff autotest.py @ 285:23981827b794

add autotest.py. It run all unittest
author Frederic Bastien <bastienf@iro.umontreal.ca>
date Fri, 06 Jun 2008 13:53:02 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/autotest.py	Fri Jun 06 13:53:02 2008 -0400
@@ -0,0 +1,54 @@
+import unittest, os, sys, traceback
+
+def test_root_dir(debugmode=False):
+    suite = None
+    filenames = os.listdir('.')
+    for filename in filenames:
+        if filename[-3:] == '.py' and filename.startswith('_test'):
+            #print >>sys.stderr, 'Loading', modname
+            modname = filename[0:-3]
+
+            try:
+                module = __import__(modname)
+            except Exception, e:
+                print >>sys.stderr, "===================================================="
+                print >>sys.stderr, "Failed to load %s.py" % modname
+                print >>sys.stderr, "===================================================="
+                traceback.print_exc()
+                print >>sys.stderr, "===================================================="
+                continue
+                
+            tests = unittest.TestLoader().loadTestsFromModule(module)
+            if tests.countTestCases() > 0:
+                print >>sys.stderr, 'Testing', modname
+                if suite is None:
+                    suite = tests
+                else:
+                    suite.addTests(tests)
+    if suite is None:
+        print >>sys.stderr, "No suite found"
+        sys.exit(1)
+    if debugmode:
+        suite.debug()
+    else:
+        unittest.TextTestRunner(verbosity=1).run(suite)
+
+if __name__ == '__main__':
+
+    def printUsage():
+        print >>sys.stderr, "Bad argument: ",sys.argv
+        print >>sys.stderr, "only --debug is supported"
+        sys.exit(1)
+    debugparam=""
+
+    if len(sys.argv)==2:
+        if sys.argv[1]=="--debug":
+            debugparam="--debug"
+            sys.argv.remove(debugparam)
+        else:
+            printUsage()
+    elif len(sys.argv)>2:
+        printUsage()
+
+    test_root_dir(debugparam!="")
+