pytest-socket
Pytest Plugin to disable socket calls during tests
What it is and what it does
pytest-socket is a pytest plugin that intercepts Python's socket interface to prevent network calls during test execution. It works by globally disabling socket access or by restricting connections to an allowlist of specific hosts, IP addresses, or CIDR ranges. You can apply these restrictions via CLI flags (like `--disable-socket`), pytest configuration files, or per-test via fixtures and markers.
The plugin is useful for ensuring tests remain isolated and deterministic by catching accidental or unintended network dependencies. It supports selective allowlisting, Unix domain socket access for async testing, and per-test overrides. The plugin has been actively maintained since 2017 and supports Python 3.10 through 3.14.
Use it for:
- Catch tests that accidentally make real HTTP requests to external APIs instead of using mocks.
- Enforce network isolation in CI/CD pipelines to ensure tests don't depend on external services.
- Allow specific tests to connect to localhost or a test server while blocking all other network access.
- Verify that async code using Unix domain sockets works correctly while blocking TCP/UDP calls.
- Prevent DNS resolution during tests to catch hardcoded hostnames or missing mock setup.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A pytest plugin that blocks or restricts socket calls during tests, preventing unintended network access and DNS resolution while allowing selective allowlisting of hosts or Unix domain sockets.
Yes. This is a focused, well-maintained tool with low install friction and no security issues. It solves a real problem—preventing accidental network calls in tests—and integrates cleanly with pytest via CLI flags, config, or fixtures. The MIT license is permissive and the active maintenance (recent release, active repo) makes it reliable for production test suites.
Install
pytest-socket on PyPI
pip
pip install pytest-socketuv
uv add pytest-socketpoetry
poetry add pytest-socketInstalling pytest-socket
Before you install
Low friction: pure Python wheel with only pytest as a runtime dependency. Actively maintained with a recent release; last commit 2026-08-11 and 342 repository stars indicate ongoing support.
License in practice
MIT license (permissive) means you can use, modify, and distribute this plugin freely in commercial and open-source projects with minimal restrictions.
Quickstart
pip install pytest-socket
# In pytest.ini or setup.cfg:
[pytest]
addopts = --disable-socket
# Or in conftest.py:
from pytest_socket import disable_socket
def pytest_runtest_setup():
disable_socket()
Requires Python 3.10 or greater and pytest 7.0 or greater.
Verify before relying
- Whether the plugin works with pytest plugins that also patch socket at module or session scope.
- Performance overhead of socket interception across large test suites.
- Compatibility with async testing frameworks beyond the documented Unix socket support.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — pytest |
| Maintenance | actively maintained — 85 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 13,881,633/month — #1,260 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_socket-0.8.0-py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
pytest-timeoutAborts pytest tests that exceed a specified…
permissive · top 1,000 on PyPI
pytest-loggingA pytest plugin that configures Python logging…
permissive · top 15,000 on PyPI
pytest-reverseA pytest plugin that runs tests in reverse…
permissive · top 15,000 on PyPI
pytest-helpers-namespaceProvides a pytest namespace for registering and…
permissive · top 15,000 on PyPI
pytest-freezegunpytest-freezegun provides a pytest plugin that…
permissive · top 5,000 on PyPI
portpickerFinds and returns an unused network port on the…
permissive · top 5,000 on PyPI
pytest-run-parallelA pytest plugin that runs test functions…
permissive · top 15,000 on PyPI
pytest-threadleakA pytest plugin that detects when tests leak…
permissive · top 15,000 on PyPI
landlockLandlock provides a Python interface to Linux's…
permissive · top 5,000 on PyPI