Wednesday, November 1, 2006

First look at test layers from zope.testing

Recently Zope team has released zope.testing version 3.0 .
For more info :
To install : # easy_install zope.testing

This is an independent testing framework which can be used outside Zope.
One of the interesting feature is test layers. I think next Zope 3 release (3.4)
will make use test layers heavily, so I started looking at this feature.

The test layer API is here:

I will show you a simple example.

First a '':

def hello():
return 'Hello'

Then '':

import hello
import unittest

class FirstLayer(object):

class TestHello(unittest.TestCase):
layer = FirstLayer
def test_hello(self):
assert hello.hello() == 'Hello'

def test_suite():
return unittest.TestSuite((unittest.makeSuite(TestHello)))

The only thing to note here is the 'layer' attribute of 'TestHello' class.
We can add 'setUp' and 'tearDown' methods to 'FirstLayer' class.

Now create a script, '':

import sys
from zope.testing import testrunner
defaults = ['--test-path', '.', '--tests-pattern', 'tests$',]

When running the script, you will get something like:

Running tests.FirstLayer tests:
Set up tests.FirstLayer in 0.000 seconds.
Ran 1 tests with 0 failures and 0 errors in 0.000 seconds.
Tearing down left over layers:
Tear down tests.FirstLayer in 0.000 seconds.