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.4.tar.gz (5.1 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.3.4-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest-socket-0.3.4.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.7 Darwin/19.4.0

File hashes

Hashes for pytest-socket-0.3.4.tar.gz
Algorithm Hash digest
SHA256 6480d6962b1f908f4b7c267ed4cfbc8fe27555562dec31cf9f8988fc1bf11f64
MD5 4de333f221846cb92414cdeef76ed40f
BLAKE2b-256 7d7121859837ff4348407c17b9f0dd4ca7278d05696c8db5844326968f254c54

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_socket-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.7 Darwin/19.4.0

File hashes

Hashes for pytest_socket-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e66de7ad7582cf23cce39f276156782c39505d20a21a705c0208b5305643a38a
MD5 59b9750292cb4d20fa2ca54baadb4280
BLAKE2b-256 c877c9d518551c1f787f0419957953345d20c8cb8b304041ca607a4376279cd6

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