| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: pytest-twisted |
| 3 |
Version: 1.14.3 |
| 4 |
Summary: A twisted plugin for pytest. |
| 5 |
Home-page: https://github.com/pytest-dev/pytest-twisted |
| 6 |
Author: Ralf Schmitt, Kyle Altendorf, Victor Titor |
| 7 |
Author-email: [email protected] |
| 8 |
Classifier: Development Status :: 5 - Production/Stable |
| 9 |
Classifier: Intended Audience :: Developers |
| 10 |
Classifier: License :: OSI Approved :: BSD License |
| 11 |
Classifier: Operating System :: OS Independent |
| 12 |
Classifier: Programming Language :: Python |
| 13 |
Classifier: Topic :: Software Development :: Testing |
| 14 |
Classifier: Programming Language :: Python :: 2 |
| 15 |
Classifier: Programming Language :: Python :: 2.7 |
| 16 |
Classifier: Programming Language :: Python :: 3 |
| 17 |
Classifier: Programming Language :: Python :: 3.6 |
| 18 |
Classifier: Programming Language :: Python :: 3.7 |
| 19 |
Classifier: Programming Language :: Python :: 3.8 |
| 20 |
Classifier: Programming Language :: Python :: 3.9 |
| 21 |
Classifier: Programming Language :: Python :: 3.10 |
| 22 |
Classifier: Programming Language :: Python :: 3.11 |
| 23 |
Classifier: Programming Language :: Python :: 3.12 |
| 24 |
Classifier: Programming Language :: Python :: Implementation :: CPython |
| 25 |
Classifier: Programming Language :: Python :: Implementation :: PyPy |
| 26 |
Requires-Python: >=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.* |
| 27 |
Description-Content-Type: text/x-rst |
| 28 |
License-File: LICENSE |
| 29 |
Requires-Dist: greenlet |
| 30 |
Requires-Dist: pytest>=2.3 |
| 31 |
Requires-Dist: decorator |
| 32 |
Provides-Extra: dev |
| 33 |
Requires-Dist: pre-commit; extra == "dev" |
| 34 |
Requires-Dist: black; extra == "dev" |
| 35 |
Provides-Extra: pyside2 |
| 36 |
Requires-Dist: qt5reactor[pyside2]>=0.6.3; extra == "pyside2" |
| 37 |
Provides-Extra: pyqt5 |
| 38 |
Requires-Dist: qt5reactor[pyqt5]>=0.6.2; extra == "pyqt5" |
| 39 |
|
| 40 |
.. -*- mode: rst; coding: utf-8 -*- |
| 41 |
|
| 42 |
============================================================================== |
| 43 |
pytest-twisted - test twisted code with pytest |
| 44 |
============================================================================== |
| 45 |
|
| 46 |
|PyPI| |Pythons| |Travis| |AppVeyor| |Actions| |Black| |
| 47 |
|
| 48 |
:Authors: Ralf Schmitt, Kyle Altendorf, Victor Titor |
| 49 |
:Version: 1.14.3 |
| 50 |
:Date: 2024-08-21 |
| 51 |
:Download: https://pypi.python.org/pypi/pytest-twisted#downloads |
| 52 |
:Code: https://github.com/pytest-dev/pytest-twisted |
| 53 |
|
| 54 |
|
| 55 |
pytest-twisted is a plugin for pytest, which allows to test code, |
| 56 |
which uses the twisted framework. test functions can return Deferred |
| 57 |
objects and pytest will wait for their completion with this plugin. |
| 58 |
|
| 59 |
|
| 60 |
NOTICE: Python 3.8 with asyncio support |
| 61 |
======================================= |
| 62 |
|
| 63 |
In Python 3.8, asyncio changed the default loop implementation to use |
| 64 |
their proactor. The proactor does not implement some methods used by |
| 65 |
Twisted's asyncio support. The result is a ``NotImplementedError`` |
| 66 |
exception such as below. |
| 67 |
|
| 68 |
.. code-block:: pytb |
| 69 |
|
| 70 |
<snip> |
| 71 |
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\asyncioreactor.py", line 320, in install |
| 72 |
reactor = AsyncioSelectorReactor(eventloop) |
| 73 |
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\asyncioreactor.py", line 69, in __init__ |
| 74 |
super().__init__() |
| 75 |
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\base.py", line 571, in __init__ |
| 76 |
self.installWaker() |
| 77 |
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\posixbase.py", line 286, in installWaker |
| 78 |
self.addReader(self.waker) |
| 79 |
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\asyncioreactor.py", line 151, in addReader |
| 80 |
self._asyncioEventloop.add_reader(fd, callWithLogger, reader, |
| 81 |
File "C:\Python38-x64\Lib\asyncio\events.py", line 501, in add_reader |
| 82 |
raise NotImplementedError |
| 83 |
NotImplementedError |
| 84 |
|
| 85 |
The previous default, the selector loop, still works but you have to |
| 86 |
explicitly set it and do so early. The following ``conftest.py`` is provided |
| 87 |
for reference. |
| 88 |
|
| 89 |
.. code-block:: python3 |
| 90 |
|
| 91 |
import sys |
| 92 |
|
| 93 |
import pytest |
| 94 |
import pytest_twisted |
| 95 |
|
| 96 |
|
| 97 |
@pytest.hookimpl(tryfirst=True) |
| 98 |
def pytest_configure(config): |
| 99 |
# https://twistedmatrix.com/trac/ticket/9766 |
| 100 |
# https://github.com/pytest-dev/pytest-twisted/issues/80 |
| 101 |
|
| 102 |
if ( |
| 103 |
config.getoption("reactor", "default") == "asyncio" |
| 104 |
and sys.platform == 'win32' |
| 105 |
and sys.version_info >= (3, 8) |
| 106 |
): |
| 107 |
import asyncio |
| 108 |
|
| 109 |
selector_policy = asyncio.WindowsSelectorEventLoopPolicy() |
| 110 |
asyncio.set_event_loop_policy(selector_policy) |
| 111 |
|
| 112 |
|
| 113 |
Python 2 support plans |
| 114 |
====================== |
| 115 |
|
| 116 |
At some point it may become impractical to retain Python 2 support. |
| 117 |
Given the small size and very low amount of development it seems |
| 118 |
likely that this will not be a near term issue. While I personally |
| 119 |
have no need for Python 2 support I try to err on the side of being |
| 120 |
helpful so support will not be explicitly removed just to not have to |
| 121 |
think about it. If major issues are reported and neither myself nor |
| 122 |
the community have time to resolve them then options will be |
| 123 |
considered. |
| 124 |
|
| 125 |
|
| 126 |
Installation |
| 127 |
============ |
| 128 |
Install the plugin as below. |
| 129 |
|
| 130 |
.. code-block:: sh |
| 131 |
|
| 132 |
pip install pytest-twisted |
| 133 |
|
| 134 |
|
| 135 |
Using the plugin |
| 136 |
================ |
| 137 |
|
| 138 |
The plugin is available after installation and can be disabled using |
| 139 |
``-p no:twisted``. |
| 140 |
|
| 141 |
By default ``twisted.internet.default`` is used to install the reactor. |
| 142 |
This creates the same reactor that ``import twisted.internet.reactor`` |
| 143 |
would. Alternative reactors can be specified using the ``--reactor`` |
| 144 |
option. This presently supports ``qt5reactor`` for use with ``pyqt5`` |
| 145 |
and ``pytest-qt`` as well as ``asyncio``. This `guide`_ describes how to add |
| 146 |
support for a new reactor. |
| 147 |
|
| 148 |
The reactor is automatically created prior to the first test but can |
| 149 |
be explicitly installed earlier by calling |
| 150 |
``pytest_twisted.init_default_reactor()`` or the corresponding function |
| 151 |
for the desired alternate reactor. |
| 152 |
|
| 153 |
|
| 154 |
inlineCallbacks |
| 155 |
=============== |
| 156 |
Using ``twisted.internet.defer.inlineCallbacks`` as a decorator for test |
| 157 |
functions, which use fixtures, does not work. Please use |
| 158 |
``pytest_twisted.inlineCallbacks`` instead. |
| 159 |
|
| 160 |
.. code-block:: python |
| 161 |
|
| 162 |
@pytest_twisted.inlineCallbacks |
| 163 |
def test_some_stuff(tmpdir): |
| 164 |
res = yield threads.deferToThread(os.listdir, tmpdir.strpath) |
| 165 |
assert res == [] |
| 166 |
|
| 167 |
|
| 168 |
ensureDeferred |
| 169 |
============== |
| 170 |
Using ``twisted.internet.defer.ensureDeferred`` as a decorator for test |
| 171 |
functions, which use fixtures, does not work. Please use |
| 172 |
``pytest_twisted.ensureDeferred`` instead. |
| 173 |
|
| 174 |
.. code-block:: python |
| 175 |
|
| 176 |
@pytest_twisted.ensureDeferred |
| 177 |
async def test_some_stuff(tmpdir): |
| 178 |
res = await threads.deferToThread(os.listdir, tmpdir.strpath) |
| 179 |
assert res == [] |
| 180 |
|
| 181 |
|
| 182 |
Waiting for deferreds in fixtures |
| 183 |
================================= |
| 184 |
``pytest_twisted.blockon`` allows fixtures to wait for deferreds. |
| 185 |
|
| 186 |
.. code-block:: python |
| 187 |
|
| 188 |
@pytest.fixture |
| 189 |
def val(): |
| 190 |
d = defer.Deferred() |
| 191 |
reactor.callLater(1.0, d.callback, 10) |
| 192 |
return pytest_twisted.blockon(d) |
| 193 |
|
| 194 |
|
| 195 |
async/await fixtures |
| 196 |
==================== |
| 197 |
``async``/``await`` fixtures can be used along with ``yield`` for normal |
| 198 |
pytest fixture semantics of setup, value, and teardown. At present only |
| 199 |
function and module scope are supported. |
| 200 |
|
| 201 |
.. code-block:: python |
| 202 |
|
| 203 |
# No yield (coroutine function) |
| 204 |
# -> use pytest_twisted.async_fixture() |
| 205 |
@pytest_twisted.async_fixture() |
| 206 |
async def foo(): |
| 207 |
d = defer.Deferred() |
| 208 |
reactor.callLater(0.01, d.callback, 42) |
| 209 |
value = await d |
| 210 |
return value |
| 211 |
|
| 212 |
# With yield (asynchronous generator) |
| 213 |
# -> use pytest_twisted.async_yield_fixture() |
| 214 |
@pytest_twisted.async_yield_fixture() |
| 215 |
async def foo_with_teardown(): |
| 216 |
d1, d2 = defer.Deferred(), defer.Deferred() |
| 217 |
reactor.callLater(0.01, d1.callback, 42) |
| 218 |
reactor.callLater(0.02, d2.callback, 37) |
| 219 |
value = await d1 |
| 220 |
yield value |
| 221 |
await d2 |
| 222 |
|
| 223 |
|
| 224 |
Hypothesis |
| 225 |
========== |
| 226 |
pytest-twisted can be used with Hypothesis. |
| 227 |
|
| 228 |
.. code-block:: python |
| 229 |
|
| 230 |
@hypothesis.given(x=hypothesis.strategies.integers()) |
| 231 |
@pytest_twisted.ensureDeferred |
| 232 |
async def test_async(x): |
| 233 |
assert isinstance(x, int) |
| 234 |
|
| 235 |
|
| 236 |
The twisted greenlet |
| 237 |
==================== |
| 238 |
Some libraries (e.g. corotwine) need to know the greenlet, which is |
| 239 |
running the twisted reactor. It's available from the |
| 240 |
``twisted_greenlet`` fixture. The following code can be used to make |
| 241 |
corotwine work with pytest-twisted. |
| 242 |
|
| 243 |
.. code-block:: python |
| 244 |
|
| 245 |
@pytest.fixture(scope="session", autouse=True) |
| 246 |
def set_MAIN(request, twisted_greenlet): |
| 247 |
from corotwine import protocol |
| 248 |
protocol.MAIN = twisted_greenlet |
| 249 |
|
| 250 |
|
| 251 |
That's (almost) all. |
| 252 |
|
| 253 |
|
| 254 |
Deprecations |
| 255 |
============ |
| 256 |
|
| 257 |
---- |
| 258 |
v1.9 |
| 259 |
---- |
| 260 |
|
| 261 |
``pytest.blockon`` |
| 262 |
Use ``pytest_twisted.blockon`` |
| 263 |
``pytest.inlineCallbacks`` |
| 264 |
Use ``pytest_twisted.inlineCallbacks`` |
| 265 |
|
| 266 |
|
| 267 |
.. |PyPI| image:: https://img.shields.io/pypi/v/pytest-twisted.svg |
| 268 |
:alt: PyPI version |
| 269 |
:target: https://pypi.python.org/pypi/pytest-twisted |
| 270 |
|
| 271 |
.. |Pythons| image:: https://img.shields.io/pypi/pyversions/pytest-twisted.svg |
| 272 |
:alt: Supported Python versions |
| 273 |
:target: https://pypi.python.org/pypi/pytest-twisted |
| 274 |
|
| 275 |
.. |Travis| image:: https://travis-ci.org/pytest-dev/pytest-twisted.svg?branch=master |
| 276 |
:alt: Travis build status |
| 277 |
:target: https://travis-ci.org/pytest-dev/pytest-twisted |
| 278 |
|
| 279 |
.. |AppVeyor| image:: https://ci.appveyor.com/api/projects/status/eb1vp9hysp463c66/branch/master?svg=true |
| 280 |
:alt: AppVeyor build status |
| 281 |
:target: https://ci.appveyor.com/project/pytestbot/pytest-twisted |
| 282 |
|
| 283 |
.. |Actions| image:: https://img.shields.io/github/workflow/status/pytest-dev/pytest-twisted/CI/master?logo=GitHub-Actions |
| 284 |
:alt: GitHub Actions build status |
| 285 |
:target: https://github.com/pytest-dev/pytest-twisted/actions?query=branch%3Amaster |
| 286 |
|
| 287 |
.. |Black| image:: https://img.shields.io/badge/code%20style-black-000000.svg |
| 288 |
:alt: Black code style |
| 289 |
:target: https://github.com/ambv/black |
| 290 |
|
| 291 |
.. _guide: CONTRIBUTING.rst |