Registry / testing / pytest-tornado

pytest-tornado

JSON →
library0.8.1pypypi✓ verified 89d ago

pytest-tornado is a plugin for the Pytest testing framework that simplifies testing asynchronous Tornado applications. It provides a set of fixtures and markers designed to integrate Tornado's asynchronous features seamlessly into pytest tests. The current version is 0.8.1, released on June 17, 2020, and the project has an infrequent release cadence.

pip install pytest-tornado
INSTALL
IMPORT
SIG · PYTEST-TORNADO
P
pytest-tornado
testingpythonv0.8.1
Install
3.2s avg
Import
424ms
Disk
37MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.8.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.444s · 34.2MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 3.2s · import 0.405s · 35MB
37MB installed
● package 37MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pytest.mark.gen_test
✓ import pytest
Used as a decorator for Tornado coroutine tests. No direct imports from `pytest_tornado` are typically needed as it provides fixtures.

This quickstart demonstrates how to test a simple Tornado web application using `pytest-tornado`. It defines a Tornado application, provides it via a `pytest` fixture named `app`, and then uses the `@pytest.mark.gen_test` marker along with `http_client` and `base_url` fixtures to perform an asynchronous HTTP request and assert the response.

import pytest import tornado.web import tornado.httpclient 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 async def test_hello_world(http_client, base_url): response = await http_client.fetch(base_url) assert response.code == 200 assert response.body == b"Hello, world" # To run: `pytest your_test_file.py`
Debug
Known issues
gotchaWhen testing native `async def` coroutines (Python 3.5+, Tornado 5.0+), the `@pytest.mark.gen_test` decorator, which is designed for `tornado.gen.coroutine` style, might feel less intuitive. While it can still be used, alternatives like `pytest-tornasync` offer direct support for `async def` without this decorator.
fix
For native `async def` tests, consider `pytest-tornasync` or ensure `@pytest.mark.gen_test` is correctly applied. For complex scenarios, you might need to combine it with `pytest-asyncio` (see next warning).
affects: All versions with Tornado >= 5.0
gotchaCombining `pytest-tornado` with `pytest-asyncio` can lead to issues if decorators are incorrectly ordered or event loops are mismanaged. The `asyncio` event loop might not run, causing tests to hang or fail with `RuntimeError: no current event loop in thread`.
fix
If using both, ensure the `@pytest.mark.asyncio` decorator, if needed to manage the event loop, wraps `@pytest.mark.gen_test` for the `asyncio` loop to be properly initiated. Alternatively, streamline your setup to primarily use one async plugin if possible, or ensure explicit `IOLoop` management if mixing concerns.
affects: All versions when used with `pytest-asyncio`
gotchaTests can hang indefinitely if a Tornado `IOLoop` instance, especially one running in a separate thread or manually managed, is not explicitly shut down after test completion. `pytest-tornado` fixtures (`io_loop`, `http_server`) typically handle this automatically for tests using them.
fix
Ensure any manually created `IOLoop` instances are properly stopped (e.g., `io_loop.stop()`, `io_loop.close()`) in test teardown or fixture finalizers. If the `IOLoop` is in another thread, ensure you stop the *correct* `IOLoop` instance from that thread.
affects: All versions, particularly with custom `IOLoop` management
Errors
Common errors & fixes
pytest hangs after tests complete
The Tornado IOLoop, especially one manually managed or running in a separate thread for a test server, was not properly shut down, preventing pytest from exiting.
fix
Ensure that any custom `tornado.ioloop.IOLoop` instances are explicitly stopped using `io_loop.stop()` and `io_loop.close()`. If the IOLoop is in a separate thread, make sure you are stopping the instance created and running within that specific thread. pytest-tornado's provided fixtures (e.g., `http_server`) should handle their IOLoop lifecycle automatically.
RuntimeError: no current event loop in thread (or similar 'Future waiting for an event loop to be running')
This error arises when `async def` functions or `asyncio`-based code is executed in a test without an active `asyncio` event loop. This often happens when mixing `pytest-tornado`'s `@pytest.mark.gen_test` with `pytest-asyncio` or native `async/await` without proper event loop management.
fix
If using `pytest-asyncio`, ensure its decorator (`@pytest.mark.asyncio`) is placed *outside* or manages the context for `@pytest.mark.gen_test` if both are applied. For simple native coroutine tests with Tornado 5.0+, consider if `pytest-tornasync` is a better fit as it handles `async def` more directly.
Deadlock or tests hanging when using external browser automation (e.g., Splinter, Selenium)
External, blocking browser drivers run synchronously and can contend with the asynchronous `IOLoop` that the Tornado application is running on within the same test context, preventing the server from processing requests.
fix
Run the Tornado application in a separate thread or process that is distinct from the main test `IOLoop` where the blocking browser automation operates. This allows the server to respond independently of the blocking client.
Upgrade
Version history
0.8.1latest on PyPI · released Jun 17, 2020
Audit
Dependencies
pytestrequiredCore testing framework
tornadorequiredAsynchronous web framework
setuptoolsrequiredBuild and distribution utility
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
pytest-tornado — pip install pytest-tornado · libregistry