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 See Build Status on Travis CI See Build Status on AppVeyor

A plugin to use with Pytest to disable 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)

Contributing

Contributions are very welcome. Tests can be run with tox, 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

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

Uploaded Source

File details

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

File metadata

File hashes

Hashes for pytest-socket-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b25297375d32661219c6fd7913dc5a9bd9d6f422b404bd4acbfd36b26b0a1546
MD5 467d0b56c7106432553c22c4f3025bc2
BLAKE2b-256 7b7c75b6d7542cd883585b334bb2e6f1f80b04bce7f4398c3cc5522e607e0240

See more details on using hashes here.

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