skillfed

pytest-socket

Pytest Plugin to disable socket calls during tests

pytest-socket v0.8.0 13.9M downloads/30d#1,260 on PyPI342
Permissive license MIT Active released

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-socket

uv

uv add pytest-socket

poetry

poetry add pytest-socket

Installing 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

Development Status :: 4 - BetaFramework :: PytestIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: Implementation :: CPythonTopic :: Software Development :: TestingTyping :: Typed

Tags

pytest network isolationdisable socket calls in testsprevent network access during testingpytest socket blockingtest network restrictionspytest DNS blockingsocket call interception
test-isolationnetwork-mocking

More Testing packages