Skip to main content

pytest-socket

PyPI current version Python Support Code style: black

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.
  • Python subprocesses are supported

Requirements

Installation

You can install pytest-subket via pip from PyPI:

pip install pytest-subket

or add to your pyproject.toml for uv:

[project.optional-dependencies]
dev = [
    "pytest-subket",
]

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

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-subket" is free and open source software

Issues

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

Acknowledgements

This is a fork of @miketheman's pytest-socket project. A fork was created to support the unique requirements of the pip project.

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

Release files for pytest-subket 0.8.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-subket 0.8.2
File Size Uploaded
pytest_subket-0.8.2.tar.gz 15.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-subket 0.8.2
File Interpreter ABI Platform
pytest_subket-0.8.2-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / pytest_subket-0.8.2.tar.gz

Download URL pytest_subket-0.8.2.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a6e1cb45f9df65b1daf614958716a6acb8278397502cb0732e6d085f2cc991e0
BLAKE2b-256 checksum
How to use checksums
d113afccdd4ed0cf2426dd8a0f4c2f367b7901947d516aed0ed56907b79f7178
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release files / pytest_subket-0.8.2-py3-none-any.whl

Download URL pytest_subket-0.8.2-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5c76a63b4ecf599536a4cd0bdc9b39326971221df67e8e8cba8d1d12cb710c7
BLAKE2b-256 checksum
How to use checksums
025f84fb701d68af82b2eebf0eaf9e59ea4b7cbd25b421adf77b8a7b975ebcf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.8.2 This release

2 release files

0.8.1

2 release files

0.8.0

2 release files

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