DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Micronaut With Relation Database and...Tests
  • Automated Application Integration With Flask, Kakfa, and API Logic Server
  • Implementing Real-Time Datadog Monitoring in Deployments
  • Improving Customer-Facing App Quality Using Tricentis Testim

Trending

  • Next-Gen IoT Performance Depends on Advanced Power Management ICs
  • Role of Cloud Architecture in Conversational AI
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Mastering Advanced Aggregations in Spark SQL
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Email Test Automation With Mailtrap

Email Test Automation With Mailtrap

Make no mistake, testing email remains a productive use of your time. However, automation can make it use less of your time.

By 
Francisco Moreno user avatar
Francisco Moreno
DZone Core CORE ·
Updated Feb. 13, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
13.1K Views

Join the DZone community and get the full member experience.

Join For Free

In most of the applications that we use today, the use of email continues to be an important part of many of the main functionalities that these systems offer us. Whether it's to register as users, recover a password, receive reports, invoices, flight reservations, accommodations, or anything else, sending emails is something that is still closely linked to the core functionality of many systems.

On the other hand, email is something that has been around for a long time. This is why we often overlook tests where sending emails is involved, either because of the complexity of the task or because we assume that there will be no errors. However, there are certain scenarios where it is advisable to perform some minimal tests. For example, some elements that you should want to check might be:

  • The proper format and design of the body of the mail.

  • The correct use and replacement of template fields.

  • That files are correctly attached.

  • Correctly sending user activation links or resetting passwords.

  • That recipients and senders are generated correctly.

Failures in any of the above elements can lead to serious security errors or a significant deterioration of the brand image in the eyes of customers.

Automated Testing for Email Sending

Mocking

When performing unit tests of the functionality in which the sending of emails intervenes, the most common is the use of some mocking framework that simulates these interactions (Mmockito, Moq, Sinon, etc.). Thanks to this we will be able to verify that our system fulfills the suitable functional specifications and behaves correctly before certain situations of error without the need to make real mailings.

Integration Tests or E2E

Although we start from a good base of unit tests, when we deal with the automation of integration tests, the management of emails is not always an issue that is easily automatable as it raises several points to take into account:

  • What accounts will receive the emails

  • What type of server receives the emails (IMAP, POP3, etc.)

  • Whether you have access/credentials for the accounts that will receive the emails.

  • How a user can filter the emails they interested in controlling.

  • Sending flow control or test synchronization.

These are just a few examples of the difficulties of automating testing when email is part of it. 

For this type of situations, Mailtrap can be very useful.

Mailtrap

Mailtrap's developers define it as a "fake SMTP testing server." In other words, they provide a service in the cloud that behaves like a real SMTP server, but does not send the emails to the final recipient. Instead, it stores them so that the corresponding verifications can be carried out.

Image title


To do this, in our system, we will have to indicate that Mailtrap is used as an SMTP server for sending mail. This connection data can be found in our user account. They even provide us with the right snippet for the most common languages.

Image title


Once we have our system configured so that the sending of emails is done through Mailtrap, how can we automate our tests? It is at this point where Mailtrap makes it really easy since it exposes an API through which we can access the appropriate mailbox to consult and operate on the emails that are on the server.

MailTrap API

All documentation concerning the Mailtrap API can be consulted online.

The fact that we can use an API to consult the mail is a great advantage for its simplicity, cleanliness, and the expressiveness that brings to the tests.

Thanks to the Mailtrap API, we will be able to read the incoming mails, eliminate them, check attachments, etc.

Example

In this link, you can see an example of a program that uses Mailtrap to send emails and the corresponding tests that verify that the mail has actually been sent.

Sending Mail

this.app.route('/').post(async (req, res) => {
            const message = Object.assign({}, req.body);
            Mail.from = message.from;
            Mail.to = message.to;
            Mail.subject = message.subject;
            Mail.message = message.message;
            const result = await Mail.sendMail();
            console.log(result);
            res.status(200).json({
                result,
            });
    });


On the server, there is a simple method that sends emails:

const transporter = nodemailer.createTransport({
            host: config.host,
            port: config.port,
            secure: false,
            auth: {
                user: config.user,
                pass: config.password,
            },
            tls: { rejectUnauthorized: false },
        });

        console.log(mailOptions);

        await transporter.sendMail(mailOptions).then((info) => {
            console.log(info);
            result = 'Envio OK';
        }).catch((err) => {
            console.log(err);
            result = err.response;
        });
        return result;


When sending mail, the "Mail.ts" class, through the nodemailer library, is in charge of sending mail using the appropriate Mailtrap configuration.

Integration Test

A possible automatic test to check the correct functioning of sending emails is also possible

Before each test deletes all the emails from an mailbox:

beforeEach((done) => {
    console.log('Clean Inbox');

    const cleanInbox = {
      url: mailtrapUrl + 'inboxes/' + inboxId + '/clean',
      headers: {
        'Api-Token': apiToken
      }
    };

    function callback(error, response, body) {
      console.log('Empty Inbox');
      expect(response.statusCode).toBe(200);
      done();
    }

    api.patch(cleanInbox, callback);
  });


The test is based on sending an email with a specific subject (based on the timestamp) and the subsequent verification that in Mailtrap, there is indeed an email with the same subject.

it('Send mail from app', (done) => {
    let timestamp = new Date();

    //Adding timestamp will help us to check if email was recieved
    let subjectText = "SendTest_" + timestamp.valueOf();
    let body = {
      from: 'TestMail@test.com',
      to: 'fran@test.com',
      subject: subjectText,
      message: 'Send Test'
    };

    //Our app will send the email
    request(app.app)
      .post('/')
      .send(body)
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200) //Check our server response
      .end(() => {
        console.log("Email sent");
        //Once server send email check mailtrap
        api.get(getEmailsEndPoint, checkMail);
      });

    const getEmailsEndPoint = {
      url: mailtrapUrl + 'inboxes/' + inboxId + '/messages',
      headers: {
        'Api-Token': apiToken
      },
    };

    let checkMail = function callback(error, response, body) {
      const info = JSON.parse(body);
      expect(response.statusCode).toBe(200);
      //Check subject
      expect(info[0].subject).toBe(subjectText);
      done();
    }
  });


Instructions to start the project

  • During development, we use both  npm run watch-ts  (recompiles application on source changes) and  npm run watch-node  (restarts application on recompilation).
    • One terminal:  npm run watch-ts 
    • Another terminal:  npm run watch-node  
  • npm run build-ts  only compiles the application.
  • npm run serve  (npm run start) only starts the application.
  • npm run test  run the tests "Send an email" and "Verification."

The complete example is available on GitHub.

unit test Test automation Mail (Apple) application API integration test Flow control (data) Integration Links

Published at DZone with permission of Francisco Moreno. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Micronaut With Relation Database and...Tests
  • Automated Application Integration With Flask, Kakfa, and API Logic Server
  • Implementing Real-Time Datadog Monitoring in Deployments
  • Improving Customer-Facing App Quality Using Tricentis Testim

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!