Skip to main content

unittest-sandbox

unittest-sandbox provides a @sandbox decorator which ensures unit test methods do not make any socket/web requests during test execution. Note: This currently only works with Python 3.

Installation

To install run: pip install unittest_sandbox

Example usage

from unittest import TestCase

import requests

from unittest_sandbox import InternetAccessBlockedException, sandbox

# The @sandbox() decorator can be applied to methods individually like below

class RequestTests(TestCase):
    def test_non_request_works(self):
        self.assertEqual(1 + 1, 2)

    @sandbox()
    def test_web_request_raises_exception_when_sandbox_decorator_is_applied(self):
        # If a web request is sent in a test method wrapped with the @sandbox decorator,
        # an InternetAccessBlockedException will be raised.

        with self.assertRaises(InternetAccessBlockedException):
            requests.get('https://www.google.com')

# The @sandbox() decorator can also be applied to the class as a whole. This is the same as decorating
# all 'test_' methods with @sandbox()

@sandbox()
class RequestTests(TestCase):
    def test_non_request_works(self):
        self.assertEqual(1 + 1, 2)

    def test_web_request_raises_exception_when_sandbox_decorator_is_applied(self):
        # If a web request is sent in a test method wrapped with the @sandbox decorator,
        # an InternetAccessBlockedException will be raised.

        with self.assertRaises(InternetAccessBlockedException):
            requests.get('https://www.google.com')

# A function can also be imported and called to prevent any web requests from occurring.
# For example, this could be called at the start of a Django settings file used by the test runner to
# ensure that no web requests are made by the tests.

from unittest_sandbox import block_socket_access

block_socket_access()

Metadata

Release files for unittest_sandbox 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unittest_sandbox 1.1.0
File Size Uploaded Provenance Actions
unittest_sandbox-1.1.0.tar.gz 2.7 kB Without provenance Details for unittest_sandbox-1.1.0.tar.gz

Release files / unittest_sandbox-1.1.0.tar.gz

Download URL unittest_sandbox-1.1.0.tar.gz
Size 2.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8bfd005ac1d61330ea238d7cd2fd45ad77572f1beb1cad7739136086a987c3ae
BLAKE2b-256 checksum
How to use checksums
ee9afb27b927b5a29c620d818051ec0e847d710b17bd07c1b8c78b7fb5c35614
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for unittest_sandbox 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page