[Zope-dev] zope.sendmail and testing

Chris Withers chris at simplistix.co.uk
Tue Aug 25 10:33:22 EDT 2009


Jan-Wijbrand Kolman wrote:
> Zope.sendmail explains in its README.txt that the developer using 
> zope.sendmail should himself take care of not sending emails (by setting 
> up a test layer for example, that would register a no-op IMailDelivery 
> utility).

Why not just use testfixtures [1] and Mock [2] to replace the SMTP class 
in your testcase?

from mock import Mock
from testfixtures import replace
@replace('smtplib.SMTP',Mock())
def test_mystuff(self):
    ...

That way you can check the right smtp calls are made, if you want to, 
and there's no chance of mail being sent?

If you're doing a lot of tests, you might want to do:

from mock import Mock
from testfixtures import Replacer

class MyTests(TestCase):

    def setUp(self):
      self.r = Replacer
      self.r.replace('smtplib.SMTP',Mock())

    def tearDown(self):
      self.r.restore()

This latter technique would probably work in a Layer too...

cheers,

Chris

[1] http://pypi.python.org/pypi/testfixtures

[2] http://pypi.python.org/pypi/mock

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


More information about the Zope-Dev mailing list