Skip to main content
https://travis-ci.org/vidartf/pytest-tornado.svg?branch=master

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

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

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

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

Release files for pytest-tornado5 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-tornado5 2.0.0
File Size Uploaded
pytest-tornado5-2.0.0.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-tornado5 2.0.0
File Interpreter ABI Platform
pytest_tornado5-2.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 14.9 kB

Release files / pytest-tornado5-2.0.0.tar.gz

Download URL pytest-tornado5-2.0.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0de5eaaff0af802a6557dd5e27ab8eab94e363c7e07a419237b9032eb8146661
BLAKE2b-256 checksum
How to use checksums
d0dc3c22d68bd33ceed02cafd32b69f4501963465e11f559d335af8781e0a03f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

Release files / pytest_tornado5-2.0.0-py2.py3-none-any.whl

Download URL pytest_tornado5-2.0.0-py2.py3-none-any.whl
Size 4.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a61164e1f34603be446c17f778b1e6d53e3153c0e68f47e632fe6184357c2f45
BLAKE2b-256 checksum
How to use checksums
5b8618862ee76649dd69416c1ff3228ca0872826cbc361edc6fb5dfa3dc3c17e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page