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
After that, set testinghome to wherever you want it, in this case a
subdirectory called "db" beneath the location of the test module.
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
from Testing import ZopeTestCase
After this, you can use the ZopeTestCase as usual:
(Post originally written by Lennart Regebro on the old Nuxeo blogs.)
Category: Product & Development