Using a special ZODB with ZopeTestCase

Sometimes you want to do tests that involve a special ZODB. The typical example of this is when you do tests for upgrades. As you can't create an old ZODB programatically without having the old products at hand, the most obvious choice is to use an old ZODB, and try the upgrade on that one. Importing a ZEXP of an old database may be an alternative, but actually using an old ZODB is closer to the real thing.

So, how do you specify which ZODB you want to use? Well, the standard way is to place a file called custom_zodb.py to provide your custom storage, but this turns out to be very tricky, as ZopeTestCase will try to provide it's own custom_zodb.py. A careful tapdance is needed, centered around the configuration variable 'testinghome'.

First in your test, import the Testing module. The testing module will, when you import it, set the testinghome variable to $SOFTWARE_HOME/lib/python/Testing:
import Testing
After that, set testinghome to wherever *you* want it, in this case a subdirectory called "db" beneath the location of the test module.
import App.config
cfg = App.config.getConfiguration()
cfg.testinghome = os.path.join(os.path.dirname(__file__), 'db')
During the import of the ZopeLite module, the Zope Application will be started, and it will now use our testinghome, find our custom_zodb.py and use our custom ZODB. So we import ZopeLite, or rather, we import ZopeTestCase, which in turn imports ZopeLite, which in turns starts Zope.
from Testing import ZopeTestCase
After this, you can use the ZopeTestCase as usual:
class MyTest(ZopeTestCase):
    ....

Important announcement: Join the Nuxeo team and contribute to the Nuxeo project! We have open positions in France and the UK for open source Java EE developers and sales engineers, both junior and senior.

Like this post? Share it:

Posted by Lennart Regebro @ 03/08/2006 04:19 PM. - Categories: zope -  1 comments

Nuxeo Bloggers: Log in!
Nuxeo - Indesko - Nuxeo 5 Project
All content is copyrighted by their author.
CPSSkins is Copyright © 2003-2006 by Jean-Marc Orliaguet. | CPS is Copyright © 2002-2006 by Nuxeo SAS.