First look at test layers from zope.testing

Recently Zope team has released zope.testing version 3.0 .
For more info : http://cheeseshop.python.org/pypi/zope.testing
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:
http://svn.zope.org/zope.testing/trunk/src/zope/testing/testrunner-layers-api.txt?view=auto

I will show you a simple example.

First a 'hello.py':

def hello():
return 'Hello'


Then 'tests.py':

import hello
import unittest

class FirstLayer(object):
pass

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, 'runtest.py':

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


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.

Popular posts from this blog

New location for Python Testing Tools Taxonomy

PyCon India 2014 received 143 proposals

To the Indian Python Programming Community, Thank you for the award