doctest or pyunit?

Fredrik Lundh fredrik at pythonware.com
Thu May 3 03:29:32 EDT 2001


Tim Peters wrote:
> So for *simple* things, unittest puts a lot of artificial constructs between
> what you're trying to test and getting it tested.  OTOH, tests that require
> repeated setup and teardown get real tedious real fast to write in doctest
> format:  since doctest is WYSIWYG, you have to show every step of every
> test explicitly in the string.

OTOH, creating a setup/teardown framework for your test
scripts isn't that hard.  here's a variant of the doctest-for-
unit-testing driver I use:

    import doctest

    test = ["myfirsttest", "mysecondtest", ...]

    for mod in tests:

        try:
            mod = __import__(mod)
        except ImportError, v:
            print "***", "skipping", mod, "(%s)" % v
            continue

        if hasattr(mod, "setup"):
            mod.setup()
        doctest.testmod(mod, report=0)
        if hasattr(mod, "teardown"):
            mod.teardown()

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

under this driver, an optional "setup" function can be used to
initialize a test environment (in global variables), and "teardown"
can be used to clean up after the test.

Cheers /F





More information about the Python-list mailing list