pytest-eventlet
pytest-eventlet is a pytest plugin that automatically applies the eventlet monkey-patch to your test suite. It simplifies testing eventlet-dependent code by ensuring the environment is correctly configured. The current version is 1.0.0, and as an initial release, its cadence is currently low but stable.
Common errors
-
ModuleNotFoundError: No module named 'eventlet'
cause The `pytest-eventlet` plugin was installed, but the `eventlet` library itself is missing.fixInstall the `eventlet` library: `pip install eventlet`. -
AssertionError: Socket module was not monkey-patched by eventlet!
cause Your test expects eventlet's monkey-patching to be active, but it wasn't applied. This can happen if `eventlet` is not installed, the plugin is not active, or the `--no-eventlet` flag was used.fixVerify `eventlet` is installed. Ensure `pytest-eventlet` is in your environment and not disabled by a pytest config or command-line flag like `--no-eventlet`. -
Tests hanging indefinitely or timing out.
cause This often indicates that `eventlet` monkey-patching is either not active, or the code under test is not written to be cooperative with eventlet (e.g., using blocking calls without yielding).fixConfirm `eventlet` patching is active using a diagnostic test (like the quickstart example). Review your application code for blocking operations that need to be made cooperative with `eventlet`.
Warnings
- gotcha pytest-eventlet relies on the `eventlet` library being installed alongside it. If `eventlet` is not present, the plugin will load without error but will not apply any monkey-patches, leading to tests that silently run without eventlet's concurrency features.
- gotcha Monkey-patching, especially global patching like eventlet's, can conflict with other concurrency libraries (e.g., gevent, asyncio) or testing frameworks that also modify standard library modules. Running tests with multiple such plugins enabled can lead to unpredictable behavior or crashes.
- gotcha The `--no-eventlet` command-line flag disables the eventlet monkey-patch. If your tests are unexpectedly failing due to blocking I/O or other eventlet-related issues, double-check that this flag is not inadvertently enabled, as it will prevent the plugin from doing its job.
Install
-
pip install pytest-eventlet eventlet
Quickstart
import socket
import eventlet
import pytest
import subprocess
import sys
# Save this as test_eventlet_patch.py
def test_socket_is_monkey_patched():
assert eventlet.patcher.is_monkey_patched('socket'), "Socket module was not monkey-patched by eventlet!"
# To run this, create a temporary file and invoke pytest
# Example of how to run this from a script:
# with open('test_eventlet_patch.py', 'w') as f:
# f.write('''
# import socket
# import eventlet
# def test_socket_is_monkey_patched():
# assert eventlet.patcher.is_monkey_patched('socket'), "Socket module was not monkey-patched by eventlet!"
# ''')
# result = subprocess.run([sys.executable, '-m', 'pytest', 'test_eventlet_patch.py'], capture_output=True, text=True)
# print(result.stdout)
# print(result.stderr)
# assert "1 passed" in result.stdout
# assert "Socket module was not monkey-patched" not in result.stdout # Ensure it passed