• Stars
    star
    287
  • Rank 144,232 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 7 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pytest Plugin to disable socket calls during tests

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.

More Repositories

1

knife-role-spaghetti

Knife plugin for Chef to draw dependency graphs for roles. Cut through the spaghetti with a knife.
Ruby
66
star
2

heroku-buildpack-datadog

Heroku Buildpack to run Datadog DogStatsD in a Dyno
Shell
55
star
3

ecs-host-service-scale

Extending ECS Auto-scaling for under $2/month with Lambda
Python
34
star
4

statsd.cr

A statsd client library for Crystal.
Crystal
33
star
5

knife-community

A Knife plugin to assist with deploying completed Chef cookbooks to the Chef Supermarket
Ruby
25
star
6

language-diff

CoffeeScript
23
star
7

awesome-taglines

Awesome list of software taglines
22
star
8

fullstack

Full-stack DevOps demo
Ruby
20
star
9

pytest-execution-timer

Measure Pytest execution phases
Python
11
star
10

melai

Melai is a ruby gem to create symlink-based package repositories
Ruby
10
star
11

ecr-metrics

Lambda Function to collect AWS ECR metrics and emit to CloudWatch
Python
8
star
12

aws-blog-feed

Example web app demonstrating AWS Copilot CLI
Python
6
star
13

cook-test

This is a place to test cookbook testing integrations
Ruby
5
star
14

boto3-keep-alive

boto3-keep-alive
Python
4
star
15

cookiecutter-fastapi-serverless

DRAFT - A cookiecutter template to generate a FastAPI service, deployable to AWS API Gateway & Lambda via the serverless framework.
Python
4
star
16

flake8-sqlalchemy

flake8-sqlalchemy
Python
3
star
17

unsilencer

Tool to remove suppressions from MailGun
Python
3
star
18

dorkbot

A Lita.io bot for New Dork City Slack community.
Ruby
3
star
19

derbywebapps

Derby Web Apps
SCSS
2
star
20

ruby_enterprise

Ruby
2
star
21

cookbooks

Chef Cookbooks
2
star
22

ecs-network-perf

Testing ECS container to container performance
Dockerfile
2
star
23

elgato.dart

A tool to interact with Elgato Lights.
Dart
2
star
24

scout_agent

A Chef Cookbook for the Scout Agent for ScoutApp.com
Ruby
1
star
25

lita-github-status

Ask your bot how GitHub is doing
Ruby
1
star
26

statusbot

A bot that can check statuses of sites
Python
1
star
27

random-code

Random Code Things
Shell
1
star
28

miketheman

My README
1
star
29

govweb

The source behind GovWeb
JavaScript
1
star
30

gitpod-es-repro

Repro for Gitpod Elasticsearch docker
1
star