HTTP server integration for testing
Project description
gocept.httpserverlayer
This package provides an HTTP server for testing your application with normal HTTP clients (e.g. a real browser). This is done using test layers, which are a feature of zope.testrunner.
gocept.httpserverlayer uses plone.testing for the test layer implementation, and exposes the following resources (accessible in your test case as self.layer[RESOURCE_NAME]):
- http_host:
The hostname of the HTTP server (Default: localhost)
- http_port:
The port of the HTTP server (Default: 0, which means chosen automatically by the operating system)
- http_address:
hostname:port, convenient to use in URLs (e.g. 'http://user:password@%s/path' % self.layer['http_address'])
This package is compatible with Python versions 2.4 - 2.7.
WSGI
This test layer takes a WSGI callable and runs it in a temporary HTTP server:
import gocept.httpserverlayer.wsgi from mypackage import App import unittest HTTP_LAYER = gocept.httpserverlayer.wsgi.Layer() HTTP_LAYER.wsgi_app = App() class WSGIExample(unittest.TestCase): layer = HTTP_LAYER def test_something(self): r = urllib.urlopen('http://%s/' % self.layer['http_address']) self.assertIn('Hello world', r.read())
You can also have a base layer provide the WSGI callable (in the wsgi_app resource):
import gocept.httpserverlayer.wsgi from mypackage import App import plone.testing class WSGILayer(plone.testing.Layer): def setUp(self): self['wsgi_app'] = App() WSGI_LAYER = WSGILayer() HTTP_LAYER = gocept.httpserverlayer.wsgi.Layer( name='HTTPLayer', bases=(WSGI_LAYER,))
Framework integration
gocept.httpserverlayer also provides integration with several web frameworks. Different frameworks require different dependencies; this is handled via setuptools extras of gocept.httpserverlayer (e.g. for Grok integration you need to require gocept.httpserverlayer[zopeappwsgi]).
Most flavours require the usage of a specialised TestCase in addition to the test layer.
Zope3 / ZTK (zope.app.testing)
Requires gocept.httpserverlayer[zopeapptesting].
This test layer builds on the usual ZCMLLayer that is used for typical ZTK functional tests:
import gocept.httpserverlayer.zopeapptesting import zope.app.testing.functional ZCML_LAYER = zope.app.testing.functional.ZCMLLayer( 'ftesting.zcml', __name__, __name__, allow_teardown=True) HTTP_LAYER = gocept.httpserverlayer.zopeapptesting.Layer( name='HTTPLayer', bases=(ZCML_LAYER,)) class ZTKExample(gocept.httpserverlayer.zopeapptesting.TestCase): layer = HTTP_LAYER def test(self): r = urllib.urlopen('http://%s/' % self.layer['http_address']) self.assertIn('Hello world', r.read())
Zope3 / ZTK / Grok (zope.app.wsgi)
Requires gocept.httpserverlayer[zopeappwsgi]
If your ZTK application uses zope.app.wsgi.testlayer (which is the recommended test setup for Grok, for example), you can use gocept.httpserverlayer.zopeappwsgi.Layer to create a WSGI app that integrates ZODB isolation, and gocept.httpserverlayer.wsgi.Layer to provide the actual HTTP server. No special TestCase is required, unittest.TestCase is enough.
The zopeappwsgi.Layer expects to find the current ZODB in the plone.testing resource zodbDB (which is used by plone.testing.zodb.EMPTY_ZODB), or you can inherit and override get_current_zodb. Here’s an example setup for Grok (which uses zope.app.appsetup.testlayer.ZODBLayer):
import gocept.httpserverlayer.wsgi import gocept.httpserverlayer.zopeappwsgi import unittest import zope.app.appsetup.testlayer ZODB_LAYER = zope.app.appsetup.testlayer.ZODBLayer( gocept.httpserverlayer.zopeappwsgi, 'testing.zcml') class WSGILayer(gocept.httpserverlayer.zopeappwsgi.Layer): defaultBases = (ZODB_LAYER,) def get_current_zodb(self): return ZODB_LAYER.db WSGI_LAYER = WSGILayer() HTTP_LAYER = gocept.httpserverlayer.wsgi.Layer( name='HTTPLayer', bases=(WSGI_LAYER,)) class GrokExample(unittest.TestCase): layer = HTTP_LAYER def test(self): r = urllib.urlopen('http://%s/' % self.layer['http_address']) self.assertIn('Hello world', r.read())
Zope 2 (ZopeTestCase)
Requires gocept.httpserverlayer[zope2testcase]
This test layer builds on Testing.ZopeTestCase.layer.ZopeLiteLayer (or nothing at all, when using Zope2<2.12):
import Products.Five.zcml import Testing.ZopeTestCase import gocept.httpserverlayer.zope2 import plone.testing class ZCMLLayer(plone.testing.Layer): def setUp(cls): Testing.ZopeTestCase.installProduct('Five') Products.Five.zcml.load_config( 'configure.zcml', package=Products.Five) Products.Five.zcml.load_config( 'configure.zcml', package=mypackage) ZCML_LAYER = ZCMLLayer() HTTP_LAYER = gocept.httpserverlayer.zope2.Layer( name='HTTPLayer', bases=(Testing.ZopeTestCase.layer.ZopeLiteLayer, ZCML_LAYER,)) class Zope2Example(gocept.httpserverlayer.zope2.TestCase): layer = HTTP_LAYER def test(self): r = urllib.urlopen('http://%s/' % self.layer['http_address']) self.assertIn('Hello world', r.read())
Zope 2 via WSGI
If your Zope 2 setup supports it, you can use the WSGI integration instead of a specialised Zope 2 integration to run your tests.
You might see the following exception when running tests:
File ".../repoze.retry-1.0-py2.7.egg/repoze/retry/__init__.py", line 55, in __call__ cl = int(cl) ValueError: invalid literal for int() with base 10: ''
To fix this issue you can use an additional middleware around your WSGI application: gocept.httpserverlayer.wsgi.FixupMiddleware.
Plone (ZopeTestCase)
Requires gocept.httpserverlayer[plonetestcase].
This test layer builds on Products.PloneTestCase.laye.PloneSiteLayer:
from Products.PloneTestCase.layer import PloneSiteLayer import Products.PloneTestCase.PloneTestCase import gocept.httpserverlayer.plone Products.PloneTestCase.PloneTestCase.setupPloneSite(id='plone') HTTP_LAYER = gocept.httpserverlayer.zope2.Layer( name='HTTPLayer', bases=(PloneSiteLayer,)) class PloneTests(gocept.httpserverlayer.plone.TestCase): layer = HTTP_LAYER def test(self): r = urllib.urlopen('http://%s/' % self.layer['http_address']) self.assertIn('Hello world', r.read())
Zope 2 / Plone (plone.testing.z2)
Requires gocept.httpserverlayer[plonetestingz2].
gocept.httpserverlayer provides a plone.testing.Layer at gocept.httpserverlayer.plonetesting.HTTP_SERVER that you can mix and match with your base layers. No special TestCase is required, unittest.TestCase is enough.
For a plain Zope2 application this might look like this (uses plone.testing[z2]):
import gocept.httpserverlayer.plonetesting import plone.testing import plone.testing.z2 class Layer(plone.testing.Layer): defaultBases = (plone.testing.z2.STARTUP,) def setUp(self): zope.configuration.xmlconfig.file( 'testing.zcml', package=mypackage, context=self['configurationContext']) Z2_LAYER = Layer() HTTP_LAYER = plone.testing.Layer( name='HTTPLayer', bases=(Z2_LAYER, gocept.httpserverlayer.plonetesting.HTTP_SERVER))
And for a Plone application like this (uses plone.app.testing):
import gocept.httpserverlayer.plonetesting import plone.app.testing.layers import plone.testing HTTP_LAYER = plone.testing.Layer( name='HTTPLayer', bases=(plone.app.testing.layers.PLONE_FIXTURE, gocept.httpserverlayer.plonetesting.HTTP_SERVER))
Developing gocept.httpserverlayer
- Author:
- Online documentation:
- PyPI page:
- Issue tracker:
https://projects.gocept.com/projects/gocept-httpserverlayer/
- Source code:
- Current change log:
https://bitbucket.org/gocept/gocept.httpserverlayer/raw/tip/CHANGES.txt
buildout configuration
gocept.httpserverlayer integrates with quite a lot of different web frameworks, so there are quite a lot of buildout configurations for this package, so we are able to run our tests against all the different software versions we integrate with. These configurations reside in the flavours/ directory, and you can employ one by editing buildout.cfg and extending it:
[buildout] extends = flavours/MY_FLAVOUR.cfg
Change log for gocept.httpserverlayer
1.0.1 (2012-12-21)
Avoid the property.setter spelling in the wsgi layer’s code for Python 2.5 compatibility.
Conditionally require wsgiref to make the wsgi layer work with Python 2.4.
Fixed an import in the plonetestcase layer’s tests.
1.0 (2012-12-19)
initial release (extracted from gocept.selenium-1.0)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for gocept.httpserverlayer-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8dcfa8f5150cb6b2dc5889f65a341c42850eb65e94f1541165107b02b9012808 |
|
MD5 | 2e7b4e56ca752a2c4e1314a0f95c7c2e |
|
BLAKE2b-256 | 4d58377766916e7b9133c0f959f8bea14f5db6897cc5b15e5799093f75ef2089 |