Setup and Tear down for automation test suite using aws-sdk and Mocha hooks

Akilaa Subramanian
3 min readOct 21, 2022

--

In Software testing, automation plays a vital role in reducing manual efforts. When talking about automation, creation and deletion of test data, popularly called Test Fixtures should not be an overhead, especially when using cloud services where every penny, every API call, every byte of storage are considered precious.

Setup and Teardown

What is Setup and Teardown?

When test engine runs test suite, it invokes each test method independently. Therefore each method must prepare and cleanup all the allocations before and after the tests are run. before() and after() are some of the hooks available to handle this.

Example Use journey to automate:

Let us consider a public API for user creation for an application like Amazon, HackerRank. Once the user is created and operations are done, these test emails, names and other info created must be deleted to get clean the test data.

User journey

Tech Stack:

  1. For API testing — axios, mocha, chai, npm, nodejs, falso(similar to faker.js), aws-sdk
  2. AWS services — API gateway, Lambda functions, DynamoDB

Implementation of Setup and Teardown of test data:

Automation process

The code to implement setup and teardown using aws-sdk deleteItem:

Refer to this git repo for the implementation.

Explanation of code block:

  1. Setup of test data ( before() )Falso is used to generate random test data, which is the user payload for testing the endpoint
  2. Test block ( it() ) — Axios is used to make a REST API call to create a user. Assertions are included here based on the response
  3. Teardown( after() ) — aws-sdk is used to perform delete operation on dynamoDB to delete the created user data
  • Since dynamoDB is used to store the user data, to delete the created test data, Javascript SDK provided by AWS can be used. For our specific use case, we need to initiate Document Client API which has the functionality to delete the item based on key and the table name passed
  • Logs can also be added wherever needed for verifying the creation and deletion process
Sample code for Deleting an item in DynamoDB table

To run the framework in local:

  1. AWS Credentials are needed to perform delete operations on the DynamoDB table — Here aws-sdk will perform the delete operation in AWS while tests are running locally
  2. Export the default AWS region as part of AWS config
  3. Environment variables if any
  4. To run the test — npx mocha test/userCreation.js

Conclusion:

Creation and cleanup of test data are made easy with the usage of hooks and aws-sdk. This avoids the overhead of manual creation and deletion of test data in case of remote storage.

This framework can be implemented using any test engine based on one’s use case and can be run in CI as well.

Paired and created by

Akilaa S & Jayanthan K J

--

--

Akilaa Subramanian

Expressing thoughts that can be shared vocally through casual blogs & Sharing IT knowledge through tech blogs.