A common function of many APIs is the ability to send notifications via email. However, this type of interaction can be tricky to test. Fortunately there are services available that can make an email inbox available via HTTP API requests and Runscope Radar is ideal for querying those APIs.
This post demonstrates how you can use Runscope to trigger an email being sent and then verify the email was actually received using the Context.IO service.
Sending An Email
As a surrogate for an API to test, I chose to use the Mandrill API to send an email.
To be able to identify that a particular email has been delivered we need to have some kind of identifying marker. To do this I created an initial variable and then assigned a random string to it.
The marker string is then added to the email subject line.

Waiting For Delivery
In order to allow time for the email to arrive in the inbox I inserted a 30 second pause in the test.
Confirming Delivery
Context.IO provides an API over Gmail and Google Apps inboxes. You will probably want to setup a separate Gmail account for testing notifications. Once you sign up for a Context.IO account you are provided with an OAuth 1.0 Consumer Key and Consumer Secret. Runscope can take care of generating the OAuth Authorization header using these values.
Within the Context.IO site you will need to connect the Gmail account. Once that is done you can use their interactive documentation pages to test the API and discover the URL to the mailbox inbox.
In order to confirm delivery we must query the email account inbox and check for a subject line with the marker value. Here is the script based assertion to do that.
var data = JSON.parse(response.body); var found = false; for (var messageidx in data) { var subject = data[messageidx].subject; var marker = variables.get("marker"); console.log(subject); if (subject.indexOf(marker) > 0) { found = true; break; } } assert(found,"Found email containing marker");
In some scenarios it may not be possible to inject a marker value into the subject line of the email. In these cases different techniques may be required to identify the delivered email. In some cases, simply checking the date and time received may be sufficient.
If you do not already have a Runscope account then why not head over to our free sign up page and start testing those hard to test parts of your API.