GitXplorerGitXplorer
m

pytest-socket

public
298 stars
44 forks
9 issues

Commits

List of commits on branch main.
Verified
cba3c86b3a1ee84bac581554a2ea9c22119245a1

chore(deps-dev): bump starlette from 0.43.0 to 0.44.0 (#400)

ddependabot[bot] committed 19 days ago
Verified
9c2900a0865a522cb9b352407e0ac4fd422f4162

chore(deps-dev): bump jinja2 from 3.1.4 to 3.1.5 in the pip group (#399)

ddependabot[bot] committed 23 days ago
Verified
609aec3d699e7c699b7f33a54340b9ad62b0249c

chore(deps-dev): bump starlette from 0.41.3 to 0.43.0 (#398)

ddependabot[bot] committed 23 days ago
Verified
a558d11008d606cab0a1dc0d346a3a15adc5dc54

[pre-commit.ci] pre-commit autoupdate (#394)

ppre-commit-ci[bot] committed a month ago
Verified
3763b0bf2c7e25ec8f3319d687e21e3f4efc0508

chore(deps-dev): bump httpx from 0.28.0 to 0.28.1 (#393)

ddependabot[bot] committed a month ago
Verified
8162998b57c19501a741c7bcd6ec99d0a3c9faa9

chore(deps): bump pytest from 8.3.3 to 8.3.4 (#392)

ddependabot[bot] committed 2 months ago

README

The README file for this repository.

pytest-socket

PyPI current version Python Support Tests pre-commit.ci status Maintainability FOSSA Status Code style: black

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:

[pytest]
addopts = --disable-socket

or add this to your setup.cfg:

[tool:pytest]
addopts = --disable-socket

or update your conftest.py to include:

from pytest_socket import disable_socket

def pytest_runtest_setup():
    disable_socket()

If you exceptionally want to enable socket for one particular execution pass --force-enable-socket. It takes precedence over --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.

Star History

Star History Chart