Wednesday, July 18, 2012

Test Doubles


Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists. In Python, None is the ultimate Dummy.


Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).


Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.


Mocks are objects that are pre-programmed with expectations which form a specification of the calls they are expected to receive.

Lifted from Mocks aren't Stubs and derived from the excellent xUnit patterns book.

Wednesday, February 8, 2012

HTTP connection timeout

You wonder how to set a timeout on your HTTP connection?
Try like this:

import socket
import urllib2

socket.setdefaulttimeout(3) # 3 seconds

request = urllib2.Request('')
response = urllib2.urlopen(req)
#urllib2.urlopen will now uses the default timeout of 3 secs.