with tools and examples
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!
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
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:
- Prepare request data (if needed)
- Make request
- 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
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 like
grep 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.)
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! ^^