[Zope] unit testing and committing transactions

Karl Anderson kra at monkey.org
Mon Mar 22 18:03:16 EST 2004


I'm trying to start introducing some unit tests to a large body of of
code.  The project is somewhat interdependent - I have to create a
Zope.app() in order to instantiate a lot of what I'm testing.  Because
of this, I'm lowering my expectations, aiming for a regression test
suite, and hoping that subsets of it can be used in a more unit
testing manner.

My current problem is that transactions will be committed during the
instantiation of some of the classes that I'm trying to test, making
it hard for me to get a clean slate each time.  My solution is to
create a new DemoStorage to be loaded from custom_zodb in the Testing
module, and calling Zope.App.startup.startup() manually (since it
won't be called more than once by default).

This works so far, but it's inefficient, and requires some slight
knowledge of the storage being used.  Is this a good idea?  Is there a
better way?

The fragment below illustrates what I'm doing.  I'm commit()ing
manually as a demonstration, which would cause the second setUp() to
complain about the existing object without a new storage being made.


def makeNewDB():
    """
    Overwrite the custom_zodb DemoStorage with a new one, then call startup
    to make a new DB.  This is needed when we want a clean slate after
    a transaction has been committed.
    """
    m = imp.find_module('custom_zodb', [TESTING_HOME])
    m = imp.load_module('Zope.custom_zodb', m[0], m[1], m[2])
    m.Storage = DemoStorage(quota=(1<<20))
    # call the app startup manually - it won't be called by Zope.startup()
    # more than once.  This only works after a call to Zope.app().
    Zope.App.startup.startup()

class testFoo(unittest.TestCase):
    def setUp(self):
        self.app = Zope.app()
        makeNewDB()
        self.app._setObject('foo', OFS.ObjectManager.ObjectManager())
        get_transaction().commit()

    def testPass(self):
        self.assertEqual(1,1)

    def testPass1(self):
        self.assertEqual(1,1)



-- 
Karl Anderson      kra at monkey.org           http://monkey.org/~kra/



More information about the Zope mailing list