Why we need emails in tests

Many applications send emails. They may be sent when a user signs up, or as part of a monthly newsletter. Whatever the reason, emails are essential for many programmers. As most developers use transactional email services, testing applications end-to-end can hit a wall when emails enter the picture.

For many use cases the need for real and unique email addresses arises.

Using test email accounts

While you can test an app once using your own email address if you want to add that test case to an automated test suite you will need programmatic access to an email address to send and receive the real emails with.

Luckily services like MailSlurp let you generate test email addresses on demand via a simple API. You can then send and receive real email using the address from within your tests.

Testing with real email addresses

If we want to test email sending and receiving end-to-end we need to generate a new email address during a test method. MailSlurp provides an official Javascript client that is easy to use. Here is an example of a test written in Javascript to demonstrate the key points.

Example

Let's imagine an application that sends emails when a user signs up. To test that we need a test that creates a new email address and uses that account to sign up for our app. Then we verify that a welcome email was received.

Let's look at the code

  • First the test-suite imports MailSlurp and instantiates with an API key
  • Next we define a test for the sign up process
  • We trigger a sign-up using the new email address from MailSlurp
  • Then we wait for the welcome message to be received and verify its contents

Testing everything

Test email accounts like MailSlurp let you test every aspect of your application. With MailSlurp you can create unlimited email addresses that are private and unique. You can then send and receive emails via REST API or official SDKs in several languages. For more information check out the documentation