Skip to main content

Pytest Plugin to disable socket calls during tests

Project description

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.

[!important] This is a fork of pytest-socket by @miketheman. Unless you are the pip project, you probably shouldn't use this. This fork was created to include subprocess support (that can even be injected into nested environments).


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.

Project details


Download files

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

Source Distribution

pytest_subket-0.8.1.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_subket-0.8.1.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for pytest_subket-0.8.1.tar.gz
Algorithm Hash digest
SHA256 f52b6df5fac8fe882542b1705c80e739dea976c792f04c5a7e44de5a91944a2f
MD5 a313c304435af1a1d0ccdcfacebc28b6
BLAKE2b-256 7a35547f7111ff0eff572f88ea90cc02b5ae409a9b7411f48366762f54deb9a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_subket-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for pytest_subket-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7dc90dcc5fd422bc04a58ecebda94e71ef524629ab17ec2d7517a38e29230e92
MD5 c22db76de7e9dd8852656de9ccec92cb
BLAKE2b-256 052cee98f189b169a2caa8557569e49bd24f24c68c6033165486a1473738e730

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page