Recently our team faced a challenge to cover user registration process by automated regression tests. Our setup is a web application with background services (that actually send out emails) and functional regression tests powered by Selenium WebDriver. Still the approach described below can be useful for testing other application types like desktop, web sites, etc.
The use case is pretty standard - user fills in some personal data on a registration form and application sends out an email to the specified address with the confirmation link containing unique token. The challenge was to automatically capture email message and extract the link.
In order to do real black-box testing approach with shared access to application database is not considered to be a suitable option. The alternative is to configure target application to either send emails via special fake SMTP server or drop them directly to a specific folder.
For manual email testing during development we've been using SMTP-Impostor for a while. There are similar tools like smtp4dev, FakeSMTP and others, but i did not find any that can be easily set up as a background service. We use dedicated test server and running desktop application all the time is not convenient (but still possible :)) in such setup.
If you're a .NET developer you get another cool option to configure your application (any application, not only web) to drop all emails to a specific pickup directory. Please find configuration magic here.
And that's it - now we can find all emails that target application sends out as .eml files in the specified directory:
The way how to read and parse .eml files will depend on the platform you're building your tests on. Further i'll describe how it can be done in .NET solution.
To parse .eml files we can first install OpenPop.NET package.
Then create storage abstraction on top of pickup directory:
And finally filter emails using LINQ:
There is one more thing that we need to think about - schedule a job to clean up old .eml files. The following powershell script may be useful:
Conclusion
Setting up an infrastructure for testing emails is pretty easy and does not heavily depend on the application type and development platform.
Hope this helps :)