SuperTest API Automation Framework with JavaScript, Node.js, Mocha, and Chai
📌 What is SuperTest?
SuperTest is a library built on top of SuperAgent that simplifies API testing by allowing HTTP assertions in Node.js. It is commonly used for REST API automation due to its ease of use and powerful capabilities.
📌 What is Mocha?
Mocha is a JavaScript test framework used for running test cases in an organized manner.
Features of Mocha
- Supports BDD (Behavior Driven Development) and TDD (Test Driven Development).
- Allows running tests asynchronously.
- Generates test reports.
- Works well with assertion libraries like Chai.
📌 What is Chai?
Chai is an assertion library used to validate API responses in tests.
Types of Assertions in Chai
- Should style => response.should.have.status(200);
- Expect style (Preferred for SuperTest) => expect(response.status).to.equal(200);
- Assert style => assert.equal(response.status, 200);
📌Test Flow
Send a request using SuperTest.
Validate response using Chai assertions.
Run tests using Mocha.
Generate reports (optional).
📌 API Testing Types Covered
Test Type Description
- Functional Testing => Ensures API returns correct responses
- Regression Testing => Verifies that new changes do not break existing functionality
- Performance Testing => Checks API response time
- Security Testing => Validates authentication and authorization mechanisms
- Data-driven Testing => Uses different input values to test API behavior
📌 Why SuperTest?
- Simple and lightweight for API testing
- Supports asynchronous requests using async/await
- Works well with Mocha and Chai for assertions
- Supports chaining HTTP methods (get(), post(), etc.)
Example code:
Folder Structure:
📌 Framework Architecture
A well-structured API automation framework consists of the following components:
- Test Runner (Mocha) => Executes test cases
- Test Scripts (SuperTest) => Makes API calls and validates responses
- Assertions (Chai) => Validates expected results
- Configuration (dotenv) => Stores environment variables
- Reporting (Mochawesome) => Generates test reports