Skip to main content

Pytest Plugin to disable socket calls during tests

Project description

https://img.shields.io/pypi/v/pytest-socket.svg https://img.shields.io/pypi/pyversions/pytest-socket.svg Python Tests Maintainability FOSSA Status

A plugin to use with Pytest to disable or restrict socket calls during tests to ensure network calls are prevented.


This Pytest plugin was generated with Cookiecutter along with @hackebrot’s Cookiecutter-pytest-plugin template.

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

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

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 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.3.5.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

pytest_socket-0.3.5-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest-socket-0.3.5.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.3 Darwin/19.5.0

File hashes

Hashes for pytest-socket-0.3.5.tar.gz
Algorithm Hash digest
SHA256 b30470fe8a0ecdbfdbfc35c1e4ac9c823f0348181570792e71ac234e07c6340f
MD5 e180d03eaf5928f4c78d00361e1c4ce8
BLAKE2b-256 a4d65dfacf9cc5683ae57b193ec2f3fb9a69dbac6f46d6f1c396d76df9199bf0

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: pytest_socket-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.3 Darwin/19.5.0

File hashes

Hashes for pytest_socket-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 405af0fcc934a82d3a4a3f45c7b059f8a353a30b4324bcd81834393bf47aae9f
MD5 f453a9892d318b36858836d268e44f19
BLAKE2b-256 cccc24c665c32b1ba3577c6005bf911a054ae1ac655e431e60b1962a844e053f

See more details on using hashes here.

Provenance

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