[Zope3-dev] Test Layers

Chris Withers chris at simplistix.co.uk
Tue Oct 17 08:13:27 EDT 2006


Jim Fulton wrote:
>> Am I right in assuming that there aren't any good narrative docs for 
>> test layers?
> 
> This is obviously in the eye of the beholder.  I'm sure the people
> who created narratives tried to do a good job. Perhaps you can do better.

I may be missing some then... which narratives are you thinking of?

And yes, I'm hoping to do better unless I've just missed some existing 
docs...

I've attached a test file which opened my eyes... how do I go about 
weaving this into a doctest?

>> Now, related to this, say I have content objects X and Y, which are 
>> expensive to set up. I have LayerX which sets up a sample content 
>> object X, and LayerY which does the same for content object Y. This is 
>> fine for tests which need one or other content type, but how do I 
>> write tests which need both?
> 
> You create a layer that extends both.

How so?

Here's a sample of why I'm struggling:

class ZODB:

    @classmethod
    def setUp(cls):
        ... open zodb connection
        ... begin transaction

    @classmethod
    def tearDown(cls):
        ... abort transaction
        ... close connection

class LayerX(ZODB):

    @classmethod
    def setUp(cls):
        cls.savepoint = transaction.savepoint()
        ... create X instance

    @classmethod
    def tearDown(cls):
        cls.savepoint.rollback()

class LayerY(ZODB):

    def setUp(cls):
        cls.savepoint = transaction.savepoint()
        ... create Y instance

    @classmethod
    def tearDown(cls):
        cls.savepoint.rollback()

class MyLayer(LayerX,LayerY): pass

class MyTests(TestCase):

    layer = '.MyLayer'

    def setUp(self):
        self.savepoint = transaction.savepoint()

    def tearDown(self):
        self.savepoint.rollback()

    def test_1(self):
        pass

Basically, will the above work or will the savepoints become a horrible 
jumbled mess and I end up with several ZODB connections as well?

cheers,

Chris

PS: can I use '.MyLayer' as a layer, or do I always need to put the full 
dotted path in?

-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk
-------------- next part --------------
from unittest import TestSuite, makeSuite, TestCase


class MyLayer:

    @classmethod
    def setUp(self):
        # do something here
        print 'L1 setup'
        print self

    @classmethod
    def tearDown(self):
        # undo it here
        print 'L1 teardown'


class MyExtendedLayer(MyLayer):

    @classmethod
    def setUp(self):
        # do additional stuff here
        # don't call super
        print 'L2 setup'

    @classmethod
    def tearDown(self):
        # undo it only the additional stuff here
        # don't call super
        print 'L2 teardown'
        raise RuntimeError('fubar MyExtendedLayer.tearDown')

class T1(TestCase):

    layer = 'Products.MyProduct.tests.test_layers.MyLayer'

    def setUp(self):
        print "T1 setup"

    def tearDown(self):
        print "T1 teardown"

    def test_1(self):
        print 'T1.1'
        
    def test_2(self):
        print 'T1.2'

    def test_3(self):
        print 'T1.3'
        raise RuntimeError('fubar T1.test_3')

class T2(TestCase):

    layer = 'Products.MyProduct.tests.test_layers.MyExtendedLayer'

    def setUp(self):
        print "T2 setup"

    def tearDown(self):
        print "T2 teardown"
        raise RuntimeError('fubar')

    def test_1(self):
        print 'T2.1'
        
    def test_2(self):
        print 'T2.2'

    def test_3(self):
        print 'T2.3'
        raise RuntimeError('fubar T2.test_3')

class T3(TestCase):

    def setUp(self):
        print "T3 setup"
        raise RuntimeError('fubar')

    def tearDown(self):
        print "T3 teardown"

    def test_1(self):
        print 'T3.1'
        
def test_suite():
    suite = TestSuite()
    suite.addTest(makeSuite(T1))
    suite.addTest(makeSuite(T2))
    suite.addTest(makeSuite(T3))
    return suite


More information about the Zope3-dev mailing list