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.
pip install pytest-tornasyncVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your project uses Tornado 5.0+ and Python 3.5+ (or newer compatible versions). Review your `requirements.txt` or `pyproject.toml` to ensure correct version pinning.
Always define an `app` fixture in your `conftest.py` or test file, returning an instance of your `tornado.web.Application`. Example: `@pytest.fixture \ndef app(): \n return your_app.make_app()`.
Remove `@pytest.mark.gen_test` from your async test functions when using `pytest-tornasync`. Simply define your tests as `async def test_something(...)`.
Ensure all async operations are properly `await`ed or managed. Use `pytest --timeout=N` or `pytest-asyncio`'s timeout features if you suspect long-running or stuck coroutines. Check for resource deadlocks or unclosed connections.
Create a fixture named `app` that returns your `tornado.web.Application`. Place this fixture in `conftest.py` or directly in your test file.
```python
import pytest
import tornado.web
@pytest.fixture
def app():
# Your Tornado application setup
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello")
return tornado.web.Application([(r"/", MainHandler)])
```Ensure your async test functions are properly defined with `async def` and receive the necessary fixtures (e.g., `io_loop`, `http_server_client`) provided by `pytest-tornasync` to operate within its managed event loop. Avoid explicit `asyncio.get_event_loop()` calls unless you are managing the loop lifecycle manually.
Verify that `pytest-tornasync` is correctly installed (`pip show pytest-tornasync`). Ensure no conflicting async plugins (like an older `pytest-tornado` that expects `@gen_test`) are enabled. Restart your pytest session to ensure plugin discovery.