For any web developer that’s ever had to build a signup routine, email is the necessary evil that takes pride of place among hated activities. Sure, a simple call to the language’s mail library will send a message through, but the moment the boss wants a HTML email, or users need attachments, everything starts to get tricky. To top things off, automating testing of an email routine can be near impossible with the usual tools. Today we’ll look at some of the options for testing email routines in web applications.
The signup validation is the bane of a web application – despite disposable emails more and more common, site owners and sales staff continue to insist on tying an email address to every user. The validation email, a common task for web developers, can quickly take up far more development time than it involves user time. Making the process of building and maintaining email routines as efficient as possible is simple better for everybody.
The challenge is this: an email, once sent, is not actually sent. For example, when PHP’s mail() function returns true, it simply means that the message has been passed on to the system’s mail server and received succesfully. The actual email could be sent many hours later, or never at all.
Fakemail, from the creators of SimpleTest, is a mail server gateway of sorts, in that it replaces your local mail server for momentarily. When an email is sent on the machine, Fakemail will pick it up and divert it to a predictable location on the filesystem.
We can then create reliable email systems in three easy steps:
1. Build a single, common email routine for the application
2. Run the email routine during build tests
3. Check the email was sent
Building a common email routine
This is the single most important factor. As your email routines grow in complexity beyond a single call to a native language API, it’s important that all your email routines use the same final message sending code, if not exactly the same emailing system altogether. For PHP, I recommend the PEAR mailing classes.
Run the email routine during build tests
Use Phing to automate your build process, if only to make sure you run your unit tests every time. Then use SimpleTest or PHPUnit (or even Selenium) to start Fakemail and then run the signup (or similar) routine, causing an email to be sent.
Check the email was sent
Finally, use a unit test or other server-side script to verify that the email was sent: assert that Fakemail created a particular file e.g. firstname.lastname@example.org will be created for the first email sent to templaterecipient@localhost.
Fakemail coexists happily with unit testing; in fact, the project website offers sample code for SimpleTest, useful for PHP developers. A similar routine in other web stacks should be fairly trivial to setup; the SimpleTest unit test is barely 20 lines long.
Testing email is not hard, and it can save a lot of stress later. To get started with Fakemail, just check out the extensive tutorial on the project page.