Pytest Trio Plugin

0.8.0 · active · verified Tue Apr 14

pytest-trio is a pytest plugin designed to facilitate testing projects that leverage Trio, a friendly library for concurrency and async I/O in Python. It enables writing asynchronous tests without boilerplate, provides useful Trio-specific fixtures like `nursery` and `autojump_clock`, and supports async fixtures. The current stable version is 0.8.0, and it follows a release cadence tied to Trio and pytest compatibility updates.

Warnings

Install

Imports

Quickstart

To quickly get started, create a `pytest.ini` file in your project root with `trio_mode = true`. This enables Trio support for all async tests. Then, write `async def` test functions. `pytest-trio` automatically provides useful fixtures like `nursery` for managing background tasks.

# pytest.ini
[pytest]
trio_mode = true

# test_example.py
import trio

async def test_sleep():
    start_time = trio.current_time()
    await trio.sleep(0.01) # Use a small sleep for quick tests
    end_time = trio.current_time()
    assert end_time - start_time >= 0.01

async def test_should_pass_with_fixture(nursery):
    results = []
    async def background_task():
        await trio.sleep(0.001)
        results.append('done')
    nursery.start_soon(background_task)
    await trio.sleep(0.002) # Ensure background task has time to run
    assert 'done' in results

# Run with: pytest test_example.py

view raw JSON →