pytest-socket
A plugin to use with Pytest to disable or restrict socket calls during
tests to ensure network calls are prevented.
Features
Disables all network calls flowing through Python's socket interface.
Requirements
Pytest 6.2.5 or greater
Installation
You can install pytest-socket via pip
from PyPI:
pip install pytest-socket
or add to your pyproject.toml for poetry:
[tool.poetry.dev-dependencies]
pytest-socket = "*"
Usage
Run pytest --disable-socket, tests should fail on any access to socket or
libraries using socket with a SocketBlockedError.
To add this flag as the default behavior, add this section to your
pytest.ini:
[pytest]
addopts = --disable-socket
or add this to your setup.cfg:
[tool:pytest]
addopts = --disable-socket
or update your conftest.py to include:
from pytest_socket import disable_socket
def pytest_runtest_setup():
disable_socket()
If you exceptionally want to enable socket for one particular execution
pass --force-enable-socket. It takes
|