This is my first time testing a socket interface using mocking. I have a class which invoked the _socket.sendto()
method. I would like to test if an exception is raised.
class socketUDP(object):
def __init__(self, host="127.0.0.1", port=2003):
self.host = host
self.port = port
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(self, metric, value):
message = "%s %f\n" % (metric, value)
try:
self._socket.sendto(
message, (self.host, self.port))
except Exception, e:
# I would like to test if this exception is raised
print "Could not relay data: %s" % e
Update:
I am writing some unit tests for the send method and one of the tests I want to do is to see if an exception was raised