Benutzerspezifische Werkzeuge

Sektionen
Unit test setup for plone 4

Direkt zum Inhalt | Direkt zur Navigation

Unit test setup for plone 4

31.03.2011 in PlanetPlone
Yesterday I spent hours trying to fix the setup of my unittests after upgrading to plone4. As usual, the solution was very trivial though hard to find.

We're currently upgrading a plone3.3.5 site to plone 4.0.4.
Whilst migration to blobstorage went very smoothly, getting the unittests to run again took ages.

Here's the important parts of the original test setup (based on the examples in "professional plone development")
base.py (module defining the base class for my unittests):

from Products.Five import zcml
from Testing import ZopeTestCase as ztc
from Products.PloneTestCase import PloneTestCase as ptc
from Products.PloneTestCase.layer import onsetup

@onsetup
def setup_product():

import my.types
zcml.load_config('configure.zcml', my.types)

# We need to tell the testing framework that these products
# should be available. This can't happen until after we have loaded
# the ZCML.
ztc.installProduct('TextIndexNG3')
ztc.installPackage('my.types')


setup_product()
ptc.setupPloneSite(products=['my.types'])

my.types package installs dependencies w/o generic-setup support (ie. TextIndexNG3) using a custom import-step
and replaces SearchableText with TextIndexNG3:

def install(context):
portal = context.getSite()
portal_quickinstaller = getToolByName(portal, 'portal_quickinstaller')
product = 'TextIndexNG3'

#install dependencies
if not portal_quickinstaller.isProductInstalled(product):
portal_quickinstaller.installProduct(product)

#replace searchabletext index
idxId = 'SearchableText'
if catalog.Indexes[idxId].meta_type != 'TextIndexNG3':
catalog.manage_delIndex(idxId)
catalog.manage_addIndex(idxId, 'TextIndexNG3',
extra={'languages': ...
...
catalog.manage_reindexIndex([idxId], portal.REQUEST)

After upgrading to plone4.0.4 I got an error while the testsuite got set up when my setuphandler was trying to add the new textindexng3. (installing the package TTW woked fine):

File ".../Products/ZCatalog/ZCatalog.py", line 960, in addIndex
raise ValueError, "Index of type %s not found" % type
ValueError: Index of type TextIndexNG3 not found

I found out, that Products.TextIndexNG3.initialize was not called, thus the index not beeing registered at the catalog.
so i did that manually in the test setup.

In addition i needed to manually load the zcml to not get ComponentLookupErrors  (eg. for txng.lexicons.default)

@onsetup
def setup_product():

import my.types
zcml.load_config('configure.zcml', my.types)

#interestingly txng's initialize is not called with ztc.installProduct(TextIndexNG3)
#so we do it manually here
from OFS.Application import install_package
app = ztc.app()
import Products.TextIndexNG3
install_package(app, Products.TextIndexNG3, Products.TextIndexNG3.initialize)

#loading zcml is required too, though the unittest formerly worked w/o
zcml.load_config('configure.zcml', Products.TextIndexNG3)

# We need to tell the testing framework that these products
# should be available. This can't happen until after we have loaded
# the ZCML.
ztc.installProduct('TextIndexNG3')
ztc.installPackage('my.types')

Not really satisfied with this solution i did some more research and found a useful hint in the german developer manual:

Starting with Plone4 ztc.installProduct must not be called within the deferred (@onsetup) method setup_product since it's too late there. The root of all evil shall be five, since it's no longer in the Products namespace.

The following setup finally works:

@onsetup
def setup_product():

import my.types
zcml.load_config('configure.zcml', my.types)

# We need to tell the testing framework that these products
# should be available. This can't happen until after we have loaded
# the ZCML.
ztc.installPackage('my.types')

#initialize products outside of the deferred (@onsetup) method, otherwise it's too late
ztc.installProduct('TextIndexNG3')

setup_product()
ptc.setupPloneSite(products=['my.types'])

I think this information should be added to the plonetestcase examples on plone.org and even more importantly to the plone upgrade guide for 4.0 to save other peoples time and nerves.

UPDATE:
I added the information to the testing section of the 4.0 upgrade-guide and asked for permission to edit the plonetestcase examples  in #11684


Kommentare unterstützt durch Disqus

Webmeisterei GmbH, Bildgasse 10d, 6850 Dornbirn, AT, Telefon +43 5572 908000, Fax +43 5572 908000-15, office@webmeisterei.com