test harness attempt

Jon Frechette jonf at connix.com
Thu May 4 10:15:03 EDT 2000

I am trying to figure out how to write a test harness for my class. I want pass a code string to a function that will print the string and then exec it. The function should be able to compare the
value of the statement against an expected value. I've have managed to get it half-way working but I can't seem to do an assignment in an 'exec' statement. Is there any way to do this ? Any help would
be greatly appreciated.

This is my object.py module:

def nObjects():
  return Object._nObjs

def _test():
  print '\ntesting object'
  _run( 'x = Object()' )
  _run( 'y = Object()' )
  _run( 'z = Object()' )
  _run( 'nObjects()', 3 )

def _run( codeStr, test = None ):
  if test == None:
    print codeStr
    exec codeStr in object.__dict__
    print codeStr,
    exec '_x_ = ' + codeStr in object.__dict__
    print '(expecting: ' + `test` + '  got: ' + `object._x_` + ')'

class Object:

  _nObjs = 0

  def __init__( self ):
    Object._nObjs = Object._nObjs + 1

if __name__ == "__main__":

More information about the Python-list mailing list