pytest-tornado
A py.test plugin providing fixtures and markers to simplify testing of asynchronous tornado applications.
Installation
pip install pytest-tornado
Example
import pytest
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
@pytest.fixture
def app():
return application
@pytest.mark.gen_test
def test_hello_world(http_client, base_url):
response = yield http_client.fetch(base_url)
assert response.code == 200
Running tests
py.test
Fixtures
- io_loop
creates an instance of the tornado.ioloop.IOLoop for each test case
- http_port
get a port used by the test server
- base_url
Get an absolute base url for the test server, for example http://localhost:59828. Can also be used in a test with HTTPS fixture and will then return a corresponding url, for example http://localhost:48372.
- http_server
start a tornado HTTP server, you must create an app fixture, which returns the tornado.web.Application to be tested
- http_client
get an asynchronous HTTP client
There is also the possibility to test applications with HTTPS. For running a server with HTTPS you need a certificate.
- https_port
Get a port used by the test server.
- https_server
Start a tornado HTTPS server. You must create an app fixture, which returns the tornado.web.Application to be tested, and an ssl_options fixture which returns the SSL options for the tornado.httpserver.HTTPServer.
- https_client
Get an asynchronous HTTP client. In case your test uses an self-signed certificate you can set verify=False on the fetch method.
Show fixtures provided by the plugin:
py.test --fixtures
Markers
A gen_test marker lets you write a coroutine-style tests used with the tornado.gen module:
@pytest.mark.gen_test
def test_tornado(http_client):
response = yield http_client.fetch('http://www.tornadoweb.org/')
assert response.code == 200
This marker supports writing tests with async/await syntax as well:
@pytest.mark.gen_test
async def test_tornado(http_client):
response = await http_client.fetch('http://www.tornadoweb.org/')
assert response.code == 200
Marked tests will time out after 5 seconds. The timeout can be modified by setting an ASYNC_TEST_TIMEOUT environment variable, --async-test-timeout command line argument or a marker argument.
@pytest.mark.gen_test(timeout=5)
def test_tornado(http_client):
yield http_client.fetch('http://www.tornadoweb.org/')
The mark can also receive a run_sync flag, which if turned off will, instead of running the test synchronously, will add it as a coroutine and run the IOLoop (until the timeout). For instance, this allows to test things on both a client and a server at the same time.
@pytest.mark.gen_test(run_sync=False)
def test_tornado(http_server, http_client):
response = yield http_client.fetch('http://localhost:5555/my_local_server_test/')
assert response.body == 'Run on the same IOLoop!'
Show markers provided by the plugin:
py.test --markers
Metadata
Release files for pytest-tornado 0.8.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 |
|---|---|---|---|---|
| pytest-tornado-0.8.1.tar.gz | 9.1 kB | Without provenance | Details for pytest-tornado-0.8.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_tornado-0.8.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for pytest_tornado-0.8.1-py2.py3-none-any.whl |
Total release size: 18.7 kB
Release files / pytest-tornado-0.8.1.tar.gz
| Download URL | pytest-tornado-0.8.1.tar.gz |
|---|---|
| Size | 9.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b9fa1930c333b9dfe29bde90f9f42e6643278b5d0c95528a8302a7454fd3f1b1
|
|
BLAKE2b-256 checksum How to use checksums |
f785a7b43fb46dbd7d6c28e798491e5f7dc902e0fbbd1630b03307529faebf79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.1
|
Release files / pytest_tornado-0.8.1-py2.py3-none-any.whl
| Download URL | pytest_tornado-0.8.1-py2.py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
8d43f400c64fabc85af58891096d4f93f05e9f9baae2c751e039932a0f62d0b5
|
|
BLAKE2b-256 checksum How to use checksums |
e77f111eb35d4f4ea7a654294b76f3d98eee6c704282fd028ae37bfc259e2216
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.1
|
Security for pytest-tornado 0.8.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.