Skip to main content

Pytest Plugin to disable socket calls during tests

Project description


PyPI current version Python Support Tests 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.


  • Disables all network calls flowing through Python's socket interface.



You can install pytest-socket via pip from PyPI:

pip install pytest-socket

or add to your pyproject.toml for poetry:

pytest-socket = "*"


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:

addopts = --disable-socket

or add this to your setup.cfg:

addopts = --disable-socket

or update your to include:

from pytest_socket import disable_socket

def pytest_runtest_setup():

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:

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)

def test_explicitly_enable_socket_with_mark():
    assert socket.socket(socket.AF_INET, socket.SOCK_STREAM)

To allow only specific hosts per-test:

def test_explicitly_enable_socket_with_mark():
    assert socket.socket.connect(('', 80))

or for whole test run

addopts = --allow-hosts=,

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.


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.


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

FOSSA Status


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


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.

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_socket-0.7.0.tar.gz (12.4 kB view hashes)

Uploaded Source

Built Distribution

pytest_socket-0.7.0-py3-none-any.whl (6.8 kB view hashes)

Uploaded Python 3

Supported by

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