From: commander_coder on 18 Feb 2010 09:37 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"
From: Roy Smith on 18 Feb 2010 09:55 In article <a9524166-5b0f-4740-8123-e2582faeeedd(a)g26g2000yqn.googlegroups.com>, commander_coder <commander_coder(a)hotmail.com> wrote: > 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.
From: commander_coder on 18 Feb 2010 10:25 On Feb 18, 9:55 am, Roy Smith <r...(a)panix.com> wrote: > 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.
From: Bruno Desthuilliers on 18 Feb 2010 10:27 commander_coder a �crit : > 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. http://docs.djangoproject.com/en/dev/topics/email/#e-mail-backends Or if you're stuck with 1.x < 1.2a, 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. My 2 cents...
From: commander_coder on 18 Feb 2010 10:59
On Feb 18, 10:27 am, Bruno Desthuilliers <bruno. 42.desthuilli...(a)websiteburo.invalid> wrote: > 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 |