socket
モジュール のいくつかのメソッドをラップする複雑なクラスをテストしたい: connect
、sendall
およびrecv
. recv
特に、このクラスのメソッドをテストしたいです。
以下の実際のコード例は、それを行う方法を示しています (単純に保つための基本的な基本的な形式でtestsocket
は、複雑なラッパー クラスに対応します)。
import socket
# This is just a socket for testing purposes, binds to the loopback device
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 1234))
sock.listen(5)
# This is the socket later part of the complex socket wrapper.
# It just contains calls to connect, sendall and recv
testsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
testsocket.connect(("127.0.0.1", 1234))
testsocket.sendall("test_send")
# The testing socket connects to a client
(client, adr) = sock.accept()
print client.recv(1024)
# Now I can do the actual test: Test the receive method of the socket
# wrapped in the complex class
client.sendall("test_recv")
print testsocket.recv(1024) # <-- This is what I want to test !!
# close everything
testsocket.close()
client.close()
sock.close()
しかし、テストするには、前testsocket.recv
に使用する必要がありますtestsocket.sendall
。
testsocket.recv
メソッドを使用せずにテストするために、このコードを簡単な方法で (フォークやスレッドを使用せずに) 変更することはできtestsocket.sendall
ますか?