Registry / testing / pytest-twisted

pytest-twisted

JSON →
library1.14.3pypypiunverified

pytest-twisted is a plugin for the pytest testing framework that facilitates testing code built with the Twisted asynchronous networking framework. It allows pytest test functions to return Twisted Deferred objects, ensuring that tests wait for asynchronous operations to complete, and manages the Twisted reactor lifecycle within the test suite. The library is actively maintained, with its current version being 1.14.3.

pip install pytest-twisted
INSTALL
IMPORT
SIG · PYTEST-TWISTED
P
pytest-twisted
testingpythonv1.14.3
Install
3.0s avg
Import
Disk
33MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.14.3 · 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.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 36.6MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.0s · import 0.000s · 34MB
33MB installed
● package 33MB
Code
Verified usage

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

inlineCallbacks
from pytest_twisted import inlineCallbacks
from pytest_twisted import inlineCallbacks

This quickstart demonstrates how to write a basic test using `pytest-twisted`. It shows a test function decorated with `pytest_twisted.inlineCallbacks` that yields a Twisted Deferred and asserts its result. A second example checks if the Twisted reactor is running within the test context. You can specify a reactor type using `--reactor` (e.g., `--reactor=asyncio`) when running pytest.

import pytest from twisted.internet.defer import Deferred from pytest_twisted import inlineCallbacks @inlineCallbacks def test_deferred_returns_value(): d = Deferred() d.callback('hello') result = yield d assert result == 'hello' @pytest.mark.reactor_default # Or specify --reactor=asyncio in pytest command @inlineCallbacks def test_reactor_is_running(): from twisted.internet import reactor assert reactor.running # To run this, save as test_example.py and run: pytest test_example.py
Debug
Known issues
deprecated`twisted.internet.defer.returnValue` is deprecated since Twisted 24.7.0. Users should migrate to using standard Python `return` statements in `inlineCallbacks` generators.
fix
Replace `returnValue(value)` with `return value` in `inlineCallbacks`-decorated functions and generators. Ensure `except:` clauses are changed to `except Exception:` to avoid catching `returnValue`'s internal `BaseException` subclass.
affects: Twisted >= 24.7.0, pytest-twisted < 1.14.3 (though 1.14.3 updated for it, user code might still use old pattern)
gotchaOn Windows with Python 3.8+ and the `asyncio` reactor, you might encounter a `NotImplementedError` due to asyncio's default proactor loop not fully supporting Twisted's `asyncio` integration.
fix
Explicitly set the `asyncio` event loop policy to `asyncio.WindowsSelectorEventLoopPolicy` early in your test suite, typically in a `conftest.py` file, to use the selector loop.

Example `conftest.py` snippet:
```python
import sys
import pytest
import asyncio

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    if (config.getoption("reactor", "default") == "asyncio" and sys.platform == 'win32' and sys.version_info >= (3, 8)):
        selector_policy = asyncio.WindowsSelectorEventLoopPolicy()
        asyncio.set_event_loop_policy(selector_policy)
```
affects: Python >= 3.8 on Windows with `--reactor=asyncio`
gotchaDirectly using `twisted.internet.defer.inlineCallbacks` or `twisted.internet.defer.ensureDeferred` as decorators for pytest test functions that rely on fixtures can lead to unexpected behavior or failures.
fix
Always use the `inlineCallbacks` and `ensureDeferred` decorators provided by `pytest_twisted` (i.e., `from pytest_twisted import inlineCallbacks`) when writing tests with fixtures.
affects: All versions
gotchaMixing `pytest` tests with `twisted.trial.unittest.TestCase` in the same test run, especially when using the `--reactor=asyncio` option, can result in a `RuntimeError: This event loop is already running`.
fix
It is generally recommended to separate tests written for `twisted.trial` from `pytest`-style tests that leverage `pytest-twisted`, or to ensure that trial tests do not attempt to manage the reactor in a way that conflicts with `pytest-twisted`'s reactor management. `pytest-twisted` is primarily designed for native `pytest` functions returning `Deferreds`.
affects: All versions, particularly when using `--reactor=asyncio`
breakingOlder versions of `pytest-twisted` might have compatibility issues with newer `pytest` releases (e.g., `pytest` 8.2.0+).
fix
Ensure `pytest-twisted` is updated to the latest version (1.14.2 or higher) to ensure compatibility with `pytest` 8.2.0 and later.
affects: pytest-twisted < 1.14.2 with pytest >= 8.2.0
breakingThe behavior of `pytest.skip()` in `twisted.trial` unittests changed with Twisted 25.5.0, potentially causing unhandled exceptions when `pytest-twisted` is used.
fix
Upgrade `pytest` to at least 8.4.1, which includes fixes for compatibility with Twisted 25+.
affects: Twisted >= 25.5.0 with pytest-twisted (specifically, older pytest versions with this Twisted version)
Upgrade
Version history
1.14.3latest on PyPI · released Sep 10, 2024
Audit
Dependencies
pytestrequiredCore testing framework that pytest-twisted extends.
TwistedrequiredThe asynchronous networking framework that pytest-twisted provides testing utilities for.
greenletrequiredRequired by pytest-twisted for managing greenlets within Twisted's cooperative multitasking model.
Agent activity
16 hits · last 30 days
node
16
Resources
pytest-twisted — pip install pytest-twisted · libregistry