[Zope3-dev] Test Layers

Jim Fulton jim at zope.com
Tue Oct 17 08:31:41 EDT 2006


Chris Withers wrote:
> 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?

You cited 3 in your note.

...

>>> 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?

You create a layer whose __bases__ include LayerX and
LayerY.  If you're layers happen to be implemented
as classic classes, you then could use subclassing.

> 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?

That's a good question. Looking at the code on the zope.testing trunk,
it looks like this should work.

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

You have to use a full dotted path.  You can also use the
Layer object itself.

Jim

-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org


More information about the Zope3-dev mailing list