Skip to main content

pytest-socket

PyPI current version Python Support Tests pre-commit.ci status Maintainability FOSSA Status 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.

Requirements

Installation

You can install pytest-socket via pip from PyPI:

$ pip install pytest-socket

or add to your pyproject.toml for poetry:

[tool.poetry.dev-dependencies]
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()

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

FOSSA Status

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.

Metadata

Release files for pytest-socket 0.5.0

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-socket 0.5.0
File Size Uploaded
pytest-socket-0.5.0.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-socket 0.5.0
File Interpreter ABI Platform
pytest_socket-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / pytest-socket-0.5.0.tar.gz

Download URL pytest-socket-0.5.0.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1a3b92c4889118c688610a8d2991b51e2893e1e519db9e4226261ce5e24f70e2
BLAKE2b-256 checksum
How to use checksums
aa27e7798ac073c17ae1e160dfa2d2f02468d20974bac23dfe755540df0d847d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.1 Darwin/21.2.0

Release files / pytest_socket-0.5.0-py3-none-any.whl

Download URL pytest_socket-0.5.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc2bfe4020255431dd9583dd657aa8b75c4f9e0c53c46566cf06ce40fe9ea0ce
BLAKE2b-256 checksum
How to use checksums
4df57406f9c3e12aad6bc63b95aa5555ec83b8cc3104bb7f0d2a5fada0774977
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.1 Darwin/21.2.0

Release history Release notifications | RSS feed

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.1

2 release files

This release

0.5.0 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release 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