test harness attempt

emile at fenx.com emile at fenx.com
Thu May 4 10:36:59 EDT 2000


I made a few tweaks, and got what appears to be what you're trying
to do (see code below).  

Your object.py module is known as main when it starts, which is why
you couldn't reference it as object.


Emile van Sebille
emile at fenx.com

Jon Frechette <jonf at connix.com> wrote in message
news:<391185E7.6CD5BC93 at connix.com>...
> 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.
<sample snipped>

import sys

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 sys.modules[__name__].__dict__
    print codeStr,
    exec '_x_ = ' + codeStr in sys.modules[__name__].__dict__
    print '(expecting: ' + `test` + '  got: ' +
`sys.modules[__name__].__dict__["_x_"]` + ')'

class Object:

  _nObjs = 0

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

if __name__ == "__main__":

More information about the Python-list mailing list