Let's say you have a client that raises MyClientTimeoutException if the underlying socket connect times out.
You can unit test this by using the HTTPretty library and stubbing out socket. This example uses Mox.
import socket
import unittest
from httpretty import HTTPretty, httprettified
import stubout
class Test(unittest.TestCase):
def setUp(self):
super(Test, self).setUp()
self.stubs = stubout.StubOutForTesting()
def tearDown(self):
super(Test, self).tearDown()
self.stubs.UnsetAll()
self.stubs.SmartUnsetAll()
@httprettified
def test_timeout_call(self):
client = MyClient()
HTTPretty.register_uri(HTTPretty.GET, "http://foo/")
# fake a timeout
def fake_create_connection(address, timeout):
raise socket.timeout('timeout')
self.stubs.Set(socket, 'create_connection', fake_create_connection)
self.assertRaises(MyClientTimeoutException,
client.call,
"http://foo/")