Mocking Email Addresses for Testing

Many people I’ve talked to know how to find a dummy credit card number for testing. And if you don’t, here’s a link: https://docs.stripe.com/testing

Fewer people that I’ve talked to know where to find dummy email addresses, and this info was more difficult for me to find online.

If you’re working in a legacy codebase or a very large codebase where you aren’t an expert in every aspect, you can’t be 100% sure that the tests never send a real email through a real SMTP server. This is especially true in integration tests. If you put in a random string with a real domain name, someone could have that email registered as a real email address, and an email with private data could accidentally be sent to them.

The ideal situation for testing is to use an email address that will never have the capability to receive emails, even if it is accidentally used in a place you don’t expect.

If we look at this fake email, test@gmail.com, it has a few sections:

  • test is the username.
  • gmail.com is the email domain name

In the email domain name, gmail is a second-level domain, and .com is a top-level domain.

Top-Level Domains

There’s a list of reserved top-level domains for special uses, listed in this Wikipedia article: https://en.wikipedia.org/wiki/Top-level_domain#Reserved_domains

This means that these domains can never be used by a real person. If someone makes a new email address, it will never use one of these reserved domains as its top-level domain.

There are two reserved domains that we care about:

  • .test is for use in testing. If you have a unit test that needs a valid email address, it should take the form of xxx@xxx.test, where xxx can be replaced by any combination of letters and numbers.
  • .example should be used for any documentation.

Second-Level domains

The list of second-level domains that are useful for development is pretty small.

These three domains are reserved despite not using any of the reserved top-level domains:

  • Example.com
  • Example.net
  • Example.org

These are all for use in examples and documentation.

Next time you do testing that requires an email address, or put an example email address in your documentation, keep your application consistent and secure by remembering these reserved domains.

For a more detailed source on top-level and second-level domains for testing, you can go here:
https://www.rfc-editor.org/rfc/rfc2606.html#section-2

author avatar
Sarah Kenny Developer
Sarah has spent most of her life in Nebraska. She graduated from the Raikes program in 2019 and graduated from the University of Nebraska-Lincoln with a degree in Computer Science one year later. She had an opportunity to intern at Don’t Panic Labs in her final year of school before transitioning to full time.

Related posts