Videos
Check out our tutorial video series.
Test Email Sign-Up and Verification with Ruby and Cucumber using MailSlurp: Test email-related processes with real emails in your Cucumber tests.
Cucumber is an extremely popular behaviour driven test framework used by developers around the world. It started as a Ruby project but now supports many languages and use cases. Cucumber tests are written in plain language and can be used to test any application. In this post we will show you how to use Cucumber to test email related processes in your application using disposable email addresses.
When writing an acceptance test using the cucumber bdd approach you may need to test email related processes. For instance, you may want to test that a user can sign-up and receive a verification email. Or you may want to test that a user can reset their password and receive a password reset email. These are common test scenarios that require real email addresses to test. But how do we do that?
MailSlurp is a free Email API that lets you generate random email addresses on demand for use in QA tests and test driven development. You can use them to send and receive real emails in a range of languages (including Ruby, Java, Javascript and more).
This post will demonstrate how to use MailSlurp with Cucumber to test the authentication features of a web application using selenium webdriver.
Cucumber tests are built around human-readable scenarios written in syntax, a form of plain english. These tests look a bit like this:
This is known as a behavior driven development bdd framework. The syntax is easy to understand and serves as documentation. This is because the piece of code describes the expectations we have for a test and document what should occur.
Note: Gherkins are very popular in Germany, where MailSlurp is based 😄.
To test email using real emails all we need to do is wire MailSlurp into our Cucumber tests. Let's write an example in Ruby to demonstrate. We could use this approach to load a web application home page, generate test email accounts, and fill a username and password form using them.
To get started make sure you have Ruby installed. Create a new project and add the following to a file.
Now run and we are ready to write some tests.
Cucumber tests live in a folder. Let's create that and add a new test feature and some step definitions.
Inside the file let's write a
scenario that tests the sending and receiving of an email in a Cucumber test.
If we run Cucumber will tell us (rightly) that we are missing step definitions for each
,
clause. We will define those next.
The magic of Cucumber tests happens in your steps files. Theses files map scenarios to real logic in your chosen programming language. Here we are using Ruby as it is popular but MailSlurp supports all other Cucumber supported languages.
Inside let's add the following:
This instantiates a MailSlurp client for our email logic with an API KEY environment variable.
⚠️ Note: MailSlurp requires an api key. You can get one free in the dashboard.
Now that MailSlurp is configured we can use it in our step definitions to create new email addresses, send real emails, and wait for emails to be received. We can then make assertions on the content of the received email.
MailSlurp supports many other features like custom domains, attachments, email pattern matching and more. See the about page for more information.
Now if we run we will see the following output!
Imagine the possibilities. Unlimited real email addresses at your disposal in Cucumber test. MailSlurp is free for personal use and available now.
You can find the full source for this post plus more on our Github page.
If you have ideas for future posts please let us know on Twitter.
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.