Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pytest_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ def disable_socket():
""" disable socket.socket to disable the Internet. useful in testing.
"""

def guarded(*args, **kwargs):
raise SocketBlockedError()
class GuardedSocket(socket.socket):
""" socket guard to disable socket creation (from pytest-socket) """
def __new__(cls, *args, **kwargs):
raise SocketBlockedError()

socket.socket = guarded
socket.socket = GuardedSocket


def enable_socket():
Expand Down
18 changes: 18 additions & 0 deletions tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,21 @@ def test_socket2():
""")
result = testdir.runpytest("--verbose", "--disable-socket")
result.assert_outcomes(1, 0, 2)


def test_socket_subclass_is_still_blocked(testdir):
testdir.makepyfile("""
import pytest
import pytest_socket
import socket

@pytest.mark.disable_socket
def test_subclass_is_still_blocked():

class MySocket(socket.socket):
pass

MySocket(socket.AF_INET, socket.SOCK_STREAM)
""")
result = testdir.runpytest("--verbose")
assert_socket_blocked(result)