view autotest.py @ 449:2bb67e978c28

updated doc
author Joseph Turian <turian@gmail.com>
date Wed, 03 Sep 2008 17:14:49 -0400
parents 23981827b794
children
line wrap: on
line source

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!="")