with tools and examples

Photo by Nathan Anderson on Unsplash

It’s great if you know about REST API and its methods, JavaScript, npm package manager, CI / CD, BDD / TDD and are familiar with API testing. If not — it’s time to read about this stuff)

Today I want to tell you how to perform REST API testing using JS, so let's start with a definition from Wikipedia:

API testing is a type of software testing that involves testing application programming interfaces (APIs) directly and as part of integration testing to determine if they meet expectations for functionality, reliability, performance, and security.

From the definition, it’s clear why API testing is important and why we should write API tests.

For now, that’s all that we need — so let’s go on!

Required tools

To set up API testing functionality we’ll need:

  • request.js — a great HTTP client, that covers 99.9% of API testing needs. It’s easy to use, fast and very functional
  • mocha — a simple and fun testing framework :)
  • chai — BDD / TDD assertion library

All these tools are free and open source! They all can be installed using npm.

Endpoint test suite example

As you may know, each REST API endpoint can have different methods, applied to it.

Let's check the basic example of the test endpoint/api/v1/users that gives us the ability to work with users.

Suppose the endpoint supports methods:

  • POST — for creating a new user
  • GET — for getting the list of created users

and additional methods on /api/v1/users/:id:

  • GET — for getting the user with specified id
  • PUT — for editing user
  • DELETE — for deleting user

All these methods can be tested in one test suite (because they all work with one UserData interface), and suite structure can look something like this:

Short and clear, like folders structure, isn’t it? :)

Endpoint tests example

Each API test is always easy to implement because most of the time it’s just about 3 operations:

  1. Prepare request data (if needed)
  2. Make request
  3. Check response

Let’s check how it may look:

Although these tests are very simple, they can give a basic understanding of how easy it can be done, and how they may look like.

The next step is to run tests and process test run results.

Test run reports and CI / CD integration

CI integration is as easy as possible :)

First, run tests using mochaCLI command.

It has tons of parameters and configurations as well as the ability to write all configurations in a file, but I won’t describe them here. You can check mocha documentation, and choose the required parameters for your own implementation. (I likegrep parameter, you should definitely check it out)

When all tests are complete, the mocha reporter will generate a test run report.

There are many reporters supported by mocha. They generate reports with different formatting and style. You can choose whichever you like.

As for me, I usually use spec reporter for the local environment (it has the best output for debugging) and XUNITreporter for the dev environment (as xUnit reports are applicable for CI tools).

When the test run report has been generated, it can be processed by a CI tool that checks the report, shows results, and performs some post-testing actions. For example:

  • If all tests are passed, the QA team receives notification that the build was successful and further testing/release tasks can be done.
  • If some tests are failed, the Dev team receives notification, with all the needed information (environment, logs, test results etc.)

Summary

In this article, I’ve shown you an example of how REST API testing can be performed using JS and its free tools.

Such API tests are short, simple, fast, and also easily maintained and integrated into your CI / CD pipelines. Moreover, they have changes history (if stored in the repository).

If you have any questions — feel free to contact me or leave a comment under this article!)

Good luck with your tests! ^^

7 years in IT. SDET, QA Lead. Love My Family, JavaScript and Quality Products

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store