Source

et-2009-provolone / elementtree / unittests / run.py

Full commit
# $Id: run.py 3303 2007-09-13 18:22:40Z fredrik $
# simple doctest runner

import os, glob, sys

try:
    import platform
    platform = platform.platform()
except ImportError:
    platform = sys.platform

TESTS = []

for file in glob.glob("unittests/test_*.py"):
    TESTS.append(os.path.basename(file)[:-3])

TESTS.sort()

if "test_sanity" in TESTS:
    TESTS.remove("test_sanity")
    TESTS.insert(0, "test_sanity")

def test():

    import sys
    sys.path.insert(0, ".")
    sys.path.insert(1, "unittests")

    import doctest

    # options (use getopt)
    verbose = 0
    try:
        sys.argv.remove("-v"); verbose = 1
    except ValueError:
        pass

    tests = []
    skips = []

    if sys.argv[1:]:
        for test in sys.argv[1:]:
            if test[:1] == "-":
                skips.append(test[1:])
            else:
                tests.append(test)

    if not tests:
        tests = TESTS

    header(verbose)

    for mod in tests:
        if mod in skips:
            print "***", "skipping", mod
            continue
        try:
            mod = __import__(mod)
        except ImportError, v:
            print "***", "skipping", mod, "(%s)" % v
            continue
        print mod.__name__, "..."
        if hasattr(mod, "setup"):
            mod.setup()
        doctest.testmod(mod, verbose=verbose, report=0)
        if hasattr(mod, "teardown"):
            mod.teardown()

    if not doctest.master:
        print "*** no tests executed"
    else:
        status = doctest.master.summarize()
        if status[0]:
            print "*** %s tests of %d failed." % status
        else:
            print "%s tests passed." % status[1]

    print "-" * 72

def header(verbose):
    import os, time, string, sys
    import support
    try:
        description = open("unittests/info.txt").readlines()
        print "-" * 72
        print string.upper(description[0][:-1])
        del description[0]
    except IOError:
        description = None
    print "-" * 72
    print "version          ", support.ET.VERSION
    print "directory        ", os.getcwd()
    if description:
        print "description      ", description[0][:-1]
        for s in description[1:]:
            print "                 ", s[:-1]
    print "time             ", time.asctime(time.localtime(time.time()))
    if os.environ.has_key("USER"):
        print "run by           ", os.environ["USER"]
    elif os.environ.has_key("LOGNAME"):
        print "run by           ", os.environ["LOGNAME"]
    print "platform         ", platform
    version = sys.version.split("[")
    print "                 ", version[0]
    if len(version) > 1:
        print "                 ", "[" + version[1]
    print "-" * 72

if __name__ == "__main__":
    if 0:
        from Test import coverage
        coverage.main("test()")
    else:
        test()