Pytest Plugin to disable socket calls during tests
Project description
pytest-socket
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
- Pytest 3.6.3 or greater
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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-socket-0.4.1.tar.gz
.
File metadata
- Download URL: pytest-socket-0.4.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.9.6 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5abcfd14d9c03e7d4e2875bb5e190af28bf5b4162d0a83e3d7aee6d79f1c85d |
|
MD5 | b56f57fc54181e73814feca1c43b2a1f |
|
BLAKE2b-256 | 776056429d871e9c6604d20d00a4706ccc60753f81623c28b6084f75e9a69c66 |
File details
Details for the file pytest_socket-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: pytest_socket-0.4.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.9.6 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3bc6cc9c2534289c6a5b9748ca733f6ae8c8d34f1fdab5115ca17a47860bf5e |
|
MD5 | 9f49b1d0dc129a0d2f5a7faa321dc3f8 |
|
BLAKE2b-256 | 8de4eaf923ecab5b7b76340181ce30a226b6716d8e16d21b91c72aea7c117d34 |