Skip to main content

tornado testlayer for use with zope.testrunner

Project description

This is a simple testlayer that can be used to test tornado web applications using the zope testrunner.

First lets create a simple sample application:

>>> from tornado.web import Application, RequestHandler
>>> class HelloWorldHandler(RequestHandler):
...     def get(self):
...         return self.write('Hello World')

Importing the layer:

>>> from tornado_testing.layer import TornadoLayer

Create the layer using the sample application created earlier:

>>> my_app = Application(handlers=[(r'/', HelloWorldHandler)])
>>> tornado_layer = TornadoLayer(my_app, port=None)
>>> type(tornado_layer.port)
<class 'int'>


If the port is None the next free port will be used.

Set up the layer:

>>> tornado_layer.setUp()

Now the layer should be running and therefore tornado will serve requests:

>>> from urllib.request import urlopen
>>> url = 'http://localhost:{}/'.format(tornado_layer.port)
>>> f = urlopen(url)
b'Hello World'

>>> f.close()
>>> tornado_layer.tearDown()


The testlayer supports patches which can be used to mock out any modules that are used inside a tornado application.

Here is an example of a tornado application that uses the os modules listdir method:

>>> import json
>>> class ListDirHandler(RequestHandler):
...     def get(self):
...         import os
...         return self.write(json.dumps(os.listdir('.')))

>>> my_app = Application(handlers=[(r'/', ListDirHandler)], debug=True)


Usually the import also works outside the method on module level. But since this example is executable using doctest it had to be put inside the method as otherwise the patch wouldn’t work.

Now define the fake listdir method:

>>> def fake_listdir(path):
...     return ['fake_file']

Create the monkey patch:

>>> from unittest.mock import patch
>>> listdir_patch = patch('os.listdir', fake_listdir)

Create the test layer using the patch:

>>> tornado_layer = TornadoLayer(my_app, port=None, patches=[listdir_patch])
>>> tornado_layer.setUp()

>>> url = 'http://localhost:{}/'.format(tornado_layer.port)
>>> f = urlopen(url)

>>> f.close()
>>> tornado_layer.tearDown()

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for tornado_testing, version 0.2.1
Filename, size File type Python version Upload date Hashes
Filename, size tornado_testing-0.2.1.tar.gz (2.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page