pytest-tornasync

0.6.0.post2 · maintenance · verified Thu Apr 16

pytest-tornasync is a simple pytest plugin that provides helpful fixtures for testing Tornado (version 5.0 or newer) applications. It simplifies testing native Python 3.5+ coroutines by eliminating the need for decorators like `@pytest.mark.gen_test`. The current version is 0.6.0.post2, with the last release in July 2019, indicating a stalled release cadence and an 'at risk' maintenance status, although the plugin remains functional for its intended purpose.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic Tornado application and test it using `pytest-tornasync`. The `app` fixture provides the `tornado.web.Application` instance, and the `http_server_client` fixture (provided by `pytest-tornasync`) is used to make asynchronous HTTP requests to the test server. Tests are defined as native Python `async def` functions.

import pytest
import tornado.web
from tornado.testing import AsyncHTTPTestCase

# Define your Tornado application
class MainHandler(tornado.web.RequestHandler):
    async def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

# Define the 'app' fixture for pytest-tornasync
@pytest.fixture
def app():
    return make_app()

# Write an async test using the http_server_client fixture
async def test_main_handler(http_server_client):
    response = await http_server_client.fetch('/')
    assert response.code == 200
    assert response.body == b"Hello, world"

view raw JSON →