[Zope] unit testing and committing transactions

Stefan H. Holek stefan at epy.co.at
Mon Mar 22 19:41:26 EST 2004


On Dienstag, Mär 23, 2004, at 00:03 Europe/Vienna, Karl Anderson wrote:

>
> 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()
>

You can replace the above with 'import Testing'. This modifies the 
configuration and Zope.app() will do the right thing (tm). Neat, huh?

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

As a simple hack you can define a tearDown() method like so:

       def tearDown(self):
           self.app._delObject('foo')
           get_transaction().commit()
           self.app._p_jar.close()

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

You may also be interested in the ZopeTestCase package which takes care 
of all these things for you: <http://zope.org/Members/shh/ZopeTestCase>

Stefan


--
The time has come to start talking about whether the emperor is as well
dressed as we are supposed to think he is.               /Pete McBreen/




More information about the Zope mailing list