Tornado Testlayer
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'>
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)
>>> f.read()
b'Hello World'
>>> f.close()
>>> tornado_layer.tearDown()
Patches
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)
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.read()
b'["fake_file"]'
>>> f.close()
>>> tornado_layer.tearDown()
Metadata
Release files for tornado_testing 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tornado_testing-0.2.1.tar.gz | 2.9 kB | Without provenance | Details for tornado_testing-0.2.1.tar.gz |
Release files / tornado_testing-0.2.1.tar.gz
| Download URL | tornado_testing-0.2.1.tar.gz |
|---|---|
| Size | 2.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bd7c76f0b797ec1ca7d5a95e90071465892938975688d14d731754d466456cbe
|
|
BLAKE2b-256 checksum How to use checksums |
71b3350ee4b741574cae80d3ef2eed79a9b44e04ce9b44a4706c47a1457bee42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for tornado_testing 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.