All computer source code presented on this page, unless it includes attribution to another author, is provided by Ed Halley under the Artistic License. Use such code freely and without any expectation of support. I would like to know if you make anything cool with the code, or need questions answered.
# testing - useful routines for running tests


A few useful routines for running unit tests.


    import testing ; from testing import *

    def my_routine(number):
        return { 'one': 1, 'two': 2, 'three': 3}[number]

    def __test__():
        __ok__( my_routine("three"), 3 )


    Ed Halley ( 17 October 2007


__all__ = [ '__ok__', '__time__', '__report__' ]


__tests = 0
__fails = 0

def __tests__(): return __tests
def __fails__(): return __fails

def __ok__(test, expect=True, message=''):
    global __tests
    global __fails
    __tests += 1
    if message: message = ' (%s)' % message
    if (test != expect):
        __fails += 1
        print "Test %d%s: [FAIL] expected %s but got %s" % \
              (__tests, message, repr(expect), repr(test))
        print "Test %d%s: [OK] %s" % \
              (__tests, message, repr(expect))

def __time__(name=None, setup='pass', stmt='pass'):
    if name is None: name = stmt
    import timeit
    timer = timeit.Timer(stmt, setup)
    number = 1000000
    timings = timer.repeat(repeat=3, number=number)
    sec = min(timings)
    desc = "%.2f us/run" % (1e6*sec/number)
    print "%18s : %s" % (desc, name)

def __report__():
    global __tests
    global __fails
    if __fails > 0:
        print 'Total of %d failed tests out of %d tests.' % \
              (__fails, __tests)
        print 'Total of %d tests.  Success.' % __tests


if __name__ == '__main__':
    raise Exception, \
        'This module is not a stand-alone script.  Import it in a program.'

Contact Ed Halley by email at
Text, code, layout and artwork are Copyright © 1996-2013 Ed Halley.
Copying in whole or in part, with author attribution, is expressly allowed.
Any references to trademarks are illustrative and are controlled by their respective owners.
Make donations with PayPal - it's fast, free and secure!