[Zope-CMF] [dev] writing tests for CMF 2.1

Chris Withers chris at simplistix.co.uk
Thu Nov 9 03:16:57 EST 2006


Hi All,

yuppie wrote:
> Layers
> ------
> 
> ZCML is set up using test layers. Their setUp() and tearDown() methods 
> are only run once for all tests in the layer.

Maybe of interest, I've attached the layers file I use when testing Zope 
2 apps. It "does the right thing" w.r.t. having a DemoStorage for each 
layer, letting you safely commit transactions containing layer setup and 
then using the using transaction.begin()/transaction.abort() dance in 
the tests themselves.

Is this code useful? Should I look to check it in to the core anywhere?

> For functional tests that depend on a complete CMF site, you can use the 
> FunctionalLayers defined in the testing modules. They have to be used 
> with ZopeTestCase's FunctionalTestCase 

Hmmm, which version of Zope did this arrive in?

Finally, a question of my own, how do you use layers with doctests?

cheers,

Chris

-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk
-------------- next part --------------
import Globals
import logging
import transaction
import Zope2

from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager
# this import, rather evilly, causes Zope2.bobo_application and Zope2.DB
# to come into existence :-(
from Testing.ZopeTestCase import ZopeLite
from Testing.ZopeTestCase.utils import makerequest
from ZODB import DB
from ZODB.DemoStorage import DemoStorage


state = [Zope2.DB]

def setChickens(db):
    # Zope 2 chickens
    Globals.BobobaseName = db.getName()
    Globals.DB = db
    Zope2.DB = db
    Globals.UndoManager=db
    ZopeLite.DB = db
    junk_db, name, version_support = Zope2.bobo_application._stuff
    Zope2.bobo_application._stuff = db, name, version_support
    
def addDB():
    global state
    previous = state[-1]
    storage = DemoStorage(base=previous._storage)    
    db = DB(storage)
    # cluck
    db.classFactory = previous.classFactory
    # cluck cluck
    setChickens(db)
    state.append(db)
    
    
def removeDB():
    global state
    db = state.pop()
    db.close()
    # un-cluck
    setChickens(state[-1])

class Layer1:

    @classmethod
    def setUp(cls):
        addDB()
        # etc
        # commit changes
        transaction.commit()
        
    @classmethod
    def tearDown(cls):
        # etc
        noSecurityManager()
        removeDB()

    
    


More information about the Zope-CMF mailing list