Skip to main content

pytest-socket

PyPI current version Python Support Tests pre-commit.ci status Maintainability Code style: black CodSpeed

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, including DNS resolution.
  • Restricts connections to an allow-list of hosts, IP addresses, or CIDR network ranges.
  • Allows Unix domain sockets selectively, for example when testing async code.
  • Applies globally via CLI flags, or per-test via fixtures and markers.

Requirements

  • Pytest 7.0 or greater
  • Python 3.10 or greater

Installation

You can install pytest-socket via pip from PyPI:

pip install pytest-socket

or add to your pyproject.toml for uv:

[project.optional-dependencies]
dev = [
    "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 precedence over --disable-socket.

To enable Unix sockets during the test run (e.g. for async), add this option:

[pytest]
addopts = --disable-socket --allow-unix-socket

To enable specific tests use of socket, pass in the fixture to the test or use a marker:

def test_explicitly_enable_socket(socket_enabled):
    assert socket.socket(socket.AF_INET, socket.SOCK_STREAM)


@pytest.mark.enable_socket
def test_explicitly_enable_socket_with_mark():
    assert socket.socket(socket.AF_INET, socket.SOCK_STREAM)

To allow only specific hosts per-test:

@pytest.mark.allow_hosts(['127.0.0.1'])
def test_explicitly_enable_socket_with_mark():
    assert socket.socket.connect(('127.0.0.1', 80))

or for whole test run

[pytest]
addopts = --allow-hosts=127.0.0.1,127.0.1.1

Entries may be hostnames, IP addresses, or CIDR network ranges such as 192.168.0.0/24.

Frequently Asked Questions

Q: Why is network access disabled in some of my tests but not others?

A: pytest's default fixture scope is "function", which socket_enabled uses. If you create another fixture that creates a socket usage that has a "higher" instantiation order, such as at the module/class/session, then the higher order fixture will be resolved first, and won't be disabled during the tests. Read more in this excellent example and more about pytest fixture order here.

This behavior may change in the future, as we learn more about pytest fixture order, and what users expect to happen.

Contributing

Contributions are very welcome. Tests can be run with pytest, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, "pytest-socket" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

References

This Pytest plugin was generated with Cookiecutter along with @hackebrot's Cookiecutter-pytest-plugin template.

This plugin came about due to the efforts by @hangtwenty solving a StackOverflow question, then converted into a pytest plugin by @miketheman.

Star History

Star History Chart

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_socket-0.8.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_socket-0.8.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest_socket-0.8.1.tar.gz.

File metadata

  • Download URL: pytest_socket-0.8.1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytest_socket-0.8.1.tar.gz
Algorithm Hash digest
SHA256 2f57787914ad2e1308d09ce141b95c3e55741fbb4fb7b7556593a6b063e0c9c7
MD5 eebfbef33da08f54c8665c9af98f8dff
BLAKE2b-256 bace4ef7b049852c95a8727b4a7e6496f762df1ac0b47bc0320d10293f5e95ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_socket-0.8.1.tar.gz:

Publisher: publish.yml on miketheman/pytest-socket

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_socket-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_socket-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytest_socket-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9846bed1dcd96eed459e5e14795bbaf96715cf4e827891fe70773817ecb8ed4
MD5 d18785714f16757363d089086a73aaea
BLAKE2b-256 87efab507f117b3d19b54e3c9c632a99c28c3b284562ec6e02e274581d530d92

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_socket-0.8.1-py3-none-any.whl:

Publisher: publish.yml on miketheman/pytest-socket

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 files

0.8.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.1

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

1 file

0.3.2

1 file

0.3.1

1 file

0.3.0

1 file

0.2.0

1 file

0.1.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page