Skip to main content

Pytest Plugin to disable socket calls during tests

Project description

pytest-socket

PyPI current version Python Support Tests Maintainability FOSSA Status

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 or setup.cfg:

[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.

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.4.0.tar.gz (5.7 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.4.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest-socket-0.4.0.tar.gz.

File metadata

  • Download URL: pytest-socket-0.4.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.2 Darwin/20.3.0

File hashes

Hashes for pytest-socket-0.4.0.tar.gz
Algorithm Hash digest
SHA256 f590593675161e1407df676f0aff9ae24ca52c19f0fb4f30df0aaf820fa32bb1
MD5 b7b96a7fd5966c27f391bfad8d217ee9
BLAKE2b-256 f73c5d56fcb4a42ac058e2f9c43c5e761888fd58fc47c8044ddc046b278aa31f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_socket-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.2 Darwin/20.3.0

File hashes

Hashes for pytest_socket-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e21faa48f1febc6ac83bf78f5dd48a8989ef84a01ae50deb3a226813e5f61d1
MD5 ca3579ccb65cc35d0b129333d6ad8abf
BLAKE2b-256 2a67fc2ec3d66e27ac9804992cdb65fc9438e8adac964a1786eab8b2533f8752

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