Skip to main content

unittest_sandbox provides a @sandbox decorator which ensures unit test methods do not make any socket/web requests during test execution.

Project description

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()

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

unittest_sandbox-1.1.0.tar.gz (2.7 kB view details)

Uploaded Source

File details

Details for the file unittest_sandbox-1.1.0.tar.gz.

File metadata

File hashes

Hashes for unittest_sandbox-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8bfd005ac1d61330ea238d7cd2fd45ad77572f1beb1cad7739136086a987c3ae
MD5 15c72f33461fc681085df4a1c0d5cd3c
BLAKE2b-256 ee9afb27b927b5a29c620d818051ec0e847d710b17bd07c1b8c78b7fb5c35614

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