FrequentlyAskedQuestions
Why is <your favourite REQUEST variable here> missing from the REQUEST?
ZTC only has a very minimal REQUEST. If you need anything fancy in the REQUEST (like SESSION, PARENTS, URL1, ...) you will have to put it there yourself, typically in afterSetUp. See e.g. testShoppingCart.py.
How do I enable the Zope event log?
To enable the event log you have to set the required environment
variables before importing ZopeTestCase
. Your test*.py
file should
begin with something like:
os.environ['EVENT_LOG_FILE'] = os.path.join(os.getcwd(), 'zLOG.log') os.environ['EVENT_LOG_SEVERITY'] = '-300' from Testing import ZopeTestCase
How do I test an existing ZODB?
Create a custom_zodb.py
file in your tests
directory that looks
like the following:
import os from ZODB.DemoStorage import DemoStorage from ZODB.FileStorage import FileStorage db = os.path.join('..', '..', '..', 'var', 'Data.fs') db = os.path.abspath(db) Storage = DemoStorage(base=FileStorage(db, read_only=1))
This way your test DemoStorage will be based on the existing FileStorage. You may safely play around in the DemoStorage without having to fear corruption of the base db.
How do I attach to a running ZEO server?
- Setup a ZEO server and start it.
- Setup a Zope instance as ZEO client. The instance directory must contain a
custom_zodb.py
file. - Set the
ZEO_INSTANCE_HOME
environment variable to point to the instance directory. - Run the tests by typing
python testBlahBlah.py
orpython runalltests.py
.
ZopeTestCase will then use the instance's custom_zodb.py
to connect to the ZEO server instead
of setting up the test DemoStorage.
Alternatively, you can add the custom_zodb.py
file to the tests
directory along the lines of the
previous example, and skip steps 2 and 3.
Background information is available here: ZopeEnvironment
Why does cut/copy/paste/clone/rename give CopyErrors?
Should you have problems with these operations make sure a _p_jar
is set up by
performing a subtransaction commit:
get_transaction().commit(1)
I found this documented in the sources of OFS/CopySupport.py
, see method _getCopy()
.
Simon Michael reports this hint did not work for him, and he had to resort to a hack and set
ZwikiPage.cb_isMoveable = lambda x:1
. I plan to look into this.
Why does import/export give AttributeErrors?
You may see "None object has no attribute ..."
errors when trying to import or export a Zope object.
Should you see such errors make sure a _p_jar
is set up by performing a subtransaction commit
like above.
Is it allowed to explicitly commit a transaction in a test?
While it is allowed to commit a subtransaction as hinted above, committing a full (complete?)
transaction is generally not recommended. Transactions are supposed to be handled
transparently by ZopeTestCase, and you have to be careful to cleanup your own mess once you call
get_transaction().commit()
inside of afterSetUp
or a test method. The testWebserver.py
example
shows how one might go about committing manually, but you will rarely want to do so - honest.