utpython : Unit Test driver for python using inspection

Download utpython.zip

Synopsis:

test.bat
test.py
ut.py
utrun.py


test.bat

Synopsis
utrun.py

test.py

Synopsis
import ut

def test_simple():
  #test ut.equals
  ut.equals(0, 0);
  ut.SelfTestAssert("");
  ut.equals(1, 0);
  ut.SelfTestAssert("test.py(7) : test_simple(): FAILED: actual: '1' expected: '0'");
  ut.equals("s1", "s1");
  ut.SelfTestAssert("");
  ut.equals('s1', 's2');
  ut.SelfTestAssert("test.py(11) : test_simple(): FAILED: actual: 's1' expected: 's2'");

def test_nulls_in_strings():
  ut.equals(None, None);
  ut.SelfTestAssert("");
  ut.equals("", None);
  ut.SelfTestAssert("test.py(17) : test_nulls_in_strings(): FAILED: actual: '' expected: 'None'");
  ut.equals(None, "");
  ut.SelfTestAssert("test.py(19) : test_nulls_in_strings(): FAILED: actual: 'None' expected: ''");
  ut.equals("", "");
  ut.SelfTestAssert("");

def test_excp_test():
  ut.equals(0, 0)
  x = 0
  x = x / x
  ut.equals(1, 1)

def test_excp_test2():
  ut.equals(0, 0);
  raise Exception("system.Exception");
  ut.equals(1, 1);

import exceptions
class myexception(exceptions.Exception):
   def __init__(self, args=None):
      self.args = args

def test_excp_test3():
  ut.equals(0, 0);
  raise myexception("myexception");
  ut.equals(1, 1);

#------------------------
class test1:
  def __str__(self):
    return 'test1'
  def test_bob(self):
     ut.equals(1, 0)
     ut.SelfTestAssert("test.py(50) : test_bob(): FAILED: actual: '1' expected: '0'");
  def test_jack(self):
     ut.equals(22, 23)
     ut.SelfTestAssert("test.py(53) : test_jack(): FAILED: actual: '22' expected: '23'");

class t2:
  def __str__(self):
    return 't2'
  def test_bob(self):
     ut.equals(1, 0)
     ut.SelfTestAssert("this ut.equals is not run");

class test2:
  test2_test_vrbl = 0
  def test_bob(self):
     inst1 = t2()
     inst2 = t2()
     ut.equals(inst1, inst1)
     ut.SelfTestAssert("");
     inst3 = test1()
     ut.equals(inst1, inst3)
     ut.SelfTestAssert("test.py(71) : test_bob(): FAILED: actual: 't2' expected: 'test1'");

test_vbrl = 0
ut.equals(test_vbrl, 0)
ut.SelfTestAssert("");
ut.equals(test_vbrl, 1)
ut.SelfTestAssert("test.py(77) : ?(): FAILED: actual: '0' expected: '1'");

def test_strings():
  s1 = 'x'
  s2 = 'x'
  ut.equals(s1, s2);
  ut.SelfTestAssert("");
  s2 = 'y'
  ut.equals(s1, s2);
  ut.SelfTestAssert("test.py(86) : test_strings(): FAILED: actual: 'x' expected: 'y'");

ut.py

Synopsis
import inspect

class Stats:
  def init(self, isbreakout):
    self.numAsserts = 0
    self.numErrors  = 0
    self.numExcps = 0
    self.numClasses = 0
    self.numFunctions = 0
    self.numMethods = 0
    self.numFiles = 0
    self.isBreakOut = isbreakout
  def report(self):
    print "Number of Files     : " + str(self.numFiles)
    if (self.isBreakOut):
      print "Number of Classes   : " + str(self.numClasses)
      print "Number of Functions : " + str(self.numFunctions)
      print "Number of Methods   : " + str(self.numMethods)
    else:
      print "Number of Tests     : " + str(self.numMethods + self.numFunctions)
    print "Number of Asserts   : " + str(self.numAsserts)
    print "Number of Errors    : " + str(self.numErrors)
    print "Number of Exceptions: " + str(self.numExcps)

gStats = Stats()
gCurrentOutputLine = ''

def equals(actual, expected):
  global gCurrentOutputLine
  gCurrentOutputLine = ''
  gStats.numAsserts = gStats.numAsserts + 1
  if (actual == expected): return
  gStats.numErrors = gStats.numErrors + 1
  fname = str(inspect.stack()[1][1])
  lineno = str(inspect.stack()[1][2])
  mname = str(inspect.stack()[1][3])
  gCurrentOutputLine = fname + "(" + lineno + ") : " + mname + "(): FAILED: actual: '" + str(actual) + "' expected: '" + str(expected) + "'"
  print gCurrentOutputLine

def SelfTestAssert(expected):
  if (gCurrentOutputLine == expected) : return
  print "SELF TEST FAILED: "
  print "   expected: '" + expected + "'"
  print "   actual  : '" + gCurrentOutputLine + "'";
  

utrun.py

Synopsis
import inspect, imp, os, ut, glob, new

class utrunner:
  def run(self, mdl):
    self.mModuleName = mdl
    self.loadModule()
    for sym in dir(self.mModule):
      self.runone(sym)
  def runone(self, sym):
    if not sym.startswith('test'): return
    exec "import " + self.mModuleName
    objname = self.mModuleName + '.' + sym
    name = objname + "()"
    obj = eval(objname)
    if inspect.isfunction(obj):
      try:
        ut.gStats.numFunctions = ut.gStats.numFunctions + 1
        exec(name)
      except Exception, msg:
        ut.gStats.numExcps = ut.gStats.numExcps + 1
        fname = self.mModuleName + ".py"
        lineno = '0'
        mname = sym + '()'
        if (sym is None) : mname = ''
        gCurrentOutputLine = fname + "(" + lineno + ") : " + mname + ": FAILED: threw exception: " + str(msg)
        print gCurrentOutputLine      
      
    elif inspect.isclass(obj):
      ut.gStats.numClasses = ut.gStats.numClasses + 1
      inst = new.instance(obj)
      for m in dir(inst):
        if not m.startswith("test"): continue
        mname = "inst." + m
        mobjname  = mname + "()"
        if callable(eval(mname)):
          exec(mobjname)
          ut.gStats.numMethods = ut.gStats.numMethods + 1
  def loadModule(self):
    (path, name) = os.path.split(self.mModuleName)
    (file, filename, data) = imp.find_module(name, [path])
    self.mModule = imp.load_module(self.mModuleName, file, filename, data)

def main(isbreakout = 0):
  ut.gStats.init(isbreakout)
  utr = utrunner()
  for f in glob.glob('test*.py'):
     ut.gStats.numFiles = ut.gStats.numFiles + 1
     name = f[0:len(f)-3];
     utr.run(name);
  ut.gStats.report()

#import profile
#profile.run("main(1)")
main(1)






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010