Last active
December 23, 2015 20:38
-
-
Save kylemcc/6690439 to your computer and use it in GitHub Desktop.
Place in project/__init__.py to patch the socket class during test runs to notify when a test attempts to connect to an external service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
class TestTriedToAccessNetwork(BaseException): | |
pass | |
if 'test' in sys.argv: | |
import socket | |
class PatchedSocket(socket.socket): | |
def __init__(self, *args, **kwargs): | |
super(PatchedSocket, self).__init__(*args, **kwargs) | |
def connect(self, address): | |
addr, _ = address | |
if addr and addr != '127.0.0.1': | |
#print "FAIL: %s" % addr | |
#traceback.print_stack() | |
raise TestTriedToAccessNetwork("Tried connecting to external resource: %s" % addr) | |
super(PatchedSocket, self).connect(address) | |
socket.socket = PatchedSocket |
stantonk
commented
Jun 23, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment