Wednesday, March 25, 2015

Poor man's unittests in WLST

Python comes with a very elaborate unittest module , and there is no reason NOT to use it, unless a) you are bound to a VERY old implementation of Python B) you are lazy and stupid like me

The good news is that it's really easy to implement a "poor man's" version of a unittest:

totalErrors = 0
totalTests = 0

def assertTrue(booleanCondition, message):
  global totalTests, totalErrors
  totalTests = totalTests + 1
  if (not booleanCondition):
    print "ERROR:", message
    totalErrors = totalErrors + 1



#your testing code here

assertTrue(domain is not None, "domain is not None")


#print test summary

print ""
print "totalTests=", totalTests
if (totalErrors == 0):
  print 'SUCCESS'
else:
  print 'FAILURE, totalErrors=', totalErrors
  





One big step forward it to use the inspect module to print the actual assertTrue statement being executed - so you avoid having to pass also the message:

import inspect

totalErrors = 0
totalTests = 0

def assertTrue(booleanCondition, message):
  global totalTests, totalErrors
  totalTests = totalTests + 1
  if (not booleanCondition):
    frame,filename,line_number,function_name,lines,index = inspect.stack()[1]
    print "ERROR:", message, frame,filename,line_number,function_name,lines,index
    totalErrors = totalErrors + 1



#your testing code here

assertTrue(domain is not None)


#print test summary

print ""
print "totalTests=", totalTests
if (totalErrors == 0):
  print 'SUCCESS'
else:
  print 'FAILURE, totalErrors=', totalErrors
  





I love being minimalistic and lazy....

No comments: