unit testing a routine that sends mail

C

commander_coder

Hello,

I have a routine that sends an email (this is how a Django view
notifies me that an event has happened). I want to unit test that
routine. So I gave each mail a unique subject line and I want to use
python's mailbox package to look for that subject. But sometimes the
mail gets delivered and sometimes it does not (I use sendmail to send
it but I believe that it is always sent since I can see an entry in
the mail log).

I thought that the mail delivery system was occasionally hitting my
mailbox lock, and that I needed to first sleep for a while. So I
wrote a test that sleeps, then grabs the mailbox and looks through it,
and if the mail is not there then it sleeps again, etc., for up to ten
tries. It is below.

However, I find that if the mail is not delivered on the first try
then it is never delivered, no matter how long I wait. So I think I
am doing mailbox wrong but I don't see how.

The real puzzler for me is that the test reliably fails every third
time. For instance, if I try it six times then it succeeds the first,
second, fourth, and fifth times. I have to say that I cannot
understand this at all but it certainly makes the unit test useless.

I'm using Python 2.6 on an Ubuntu system. If anyone could give me a
pointer to why the mail is not delivered, I sure could use it.

Thanks,
Jim

....................................................................

class sendEmail_test(unittest.TestCase):
"""Test sendEmail()
"""
config = ConfigParser.ConfigParser()
config.read(CONFIG_FN)
mailbox_fn=config.get('testing','mailbox')

def look_in_mailbox(self,uniquifier='123456789'):
"""If the mailbox has a message whose subject line contains
the
given uniquifier then it returns that message and deletes it.
Otherwise, returns None.
"""
sleep_time=10.0 # wait for message to be delivered?
message_found=None
i=0
while i<10 and not(message_found):
time.sleep(sleep_time)
m=mailbox.mbox(self.mailbox_fn)
try:
m.lock()
except Exception, err:
print "trouble locking the mailbox: "+str(err)
try:
for key,message in m.items():
subject=message['Subject'] or ''
print "subject is ",subject
if subject.find(uniquifier)>-1:
print "+++found the message+++ i=",str(i)
message_found=message
m.remove(key)
break
m.flush()
except Exception, err:
print "trouble reading from the mailbox: "+str(err)
m.unlock()
try:
m.unlock()
except Exception, err:
print "trouble unlocking the mailbox: "+str(err)
try:
m.close()
except Exception, err:
print "trouble closing the mailbox: "+str(err)
del m
i+=1
return message_found

def test_mailbox(self):
random.seed()
uniquifier=str(int(random.getrandbits(20)))
print "uniquifier is ",uniquifier # looks different every
time to me
rc=az_view.sendEmail(uniquifier=uniquifier)
if rc:
self.fail('rc is not None: '+str(rc))
found=self.look_in_mailbox(uniquifier)
if not(found):
self.fail('message not found')
print "done"
 
R

Roy Smith

commander_coder said:
The real puzzler for me is that the test reliably fails every third
time. For instance, if I try it six times then it succeeds the first,
second, fourth, and fifth times. I have to say that I cannot
understand this at all but it certainly makes the unit test useless.

Just a wild guess here, but maybe there's some DNS server which
round-robins three address records for some hostname you're using, one of
which is bogus.

I've seen that before, and this smells like the right symptoms.
 
C

commander_coder

Just a wild guess here, but maybe there's some DNS server which
round-robins three address records for some hostname you're using, one of
which is bogus.

I've seen that before, and this smells like the right symptoms.

Everything happens on my laptop, localhost, where I'm developing.

I'm sorry; I wasn't sure how much information was too much (or too
little) but I should have said that.

Thank you.
 
C

commander_coder

you could just mock the send_mail
function to test that your app does send the appropriate mail - which is
what you really want to know.

That's essentially what I think I am doing.

I need to send a relatively complex email, multipart, with both a
plain text and html versions of a message, and some attachments. I am
worried that the email would fail to get out for some reason (say,
attaching the html message fails), so I want to test it. I started
out with a simple email, thinking to get unit tests working and then
as I add stuff to the email I'd run the tests to see if it broke
anything.

I could mock the send_mail function by having it print to the screen
"mail sent" or I could have a unit test that looked for the mail. I
did the first, and it worked fine. I thought to do the second,
starting with a unit test checking simply that the message got to the
mailbox. But I am unable to check that, as described in the original
message.

Jim
 
C

commander_coder

Bruno, I talked to someone who explained to me how what you said
gives a way around my difficulty. Please ignore the other reply.
I'll do what you said. Thank you; I appreciate your help.

Jim
 
P

Phlip

commander_coder said:
I have a routine that sends an email (this is how a Django view
notifies me that an event has happened).  I want to unit test that
routine.

Are you opening SMTP and POP3 sockets??

If you are not developing that layer itself, just use Django's built-
in mock system. Here's my favorite assertion for it:

def assert_mail(self, funk):
from django.core import mail
previous_mails = len(mail.outbox)
funk()
mails = mail.outbox[ previous_mails : ]
assert [] != mails, 'the called block should produce emails'
if len(mails) == 1: return mails[0]
return mails

You call it a little bit like this:

missive = self.assert_mail( lambda:
mark_order_as_shipped(order) )

Then you can use assert_contains on the missive.body, to see what mail
got generated. That's what you are actually developing, so your tests
should skip any irrelevant layers, and only test the layers where you
yourself might add bugs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top