Welcome to the ultimate guide for getting started with NoSwag—the modern, lightweight, and powerful API testing tool designed for developers and QA engineers. Whether you're new to API testing or looking to streamline your workflow, this guide will walk you through everything you need to know to set up NoSwag, configure it for your testing needs, and run your first successful tests.
In this post, you’ll learn:
Let’s dive in!
NoSwag is a Swagger/OpenAPI-based API testing framework that simplifies the process of testing RESTful APIs. Unlike traditional tools, NoSwag leverages OpenAPI specifications to generate test cases, validate responses, and automate API verification—all while maintaining readability and ease of use.
Before installing NoSwag, ensure you have:
Install NoSwag via npm
npm install -g noswag
(Or use yarn global add noswag if you prefer Yarn.)
Verify Installation Run the following command to check if NoSwag is installed correctly:
noswag --version
Initialize a NoSwag Project Navigate to your project directory and run:
noswag init
This will generate a default noswag.config.js file.
The noswag.config.js file is where you define test settings, API endpoints, and custom behaviors.
module.exports = {
api: {
baseUrl: "https://api.example.com/v1",
openApiSpec: "./swagger.json" // Path to your OpenAPI file
},
tests: {
validateStatusCode: true, // Automatically check for 2xx responses
timeout: 5000, // Test timeout in milliseconds
},
reporters: ["json", "cli"], // Output formats
environment: {
authToken: process.env.API_TOKEN, // Use environment variables
}
};
| Option | Description |
|---|---|
baseUrl | The root URL for API requests. |
openApiSpec | Path to the OpenAPI/Swagger file. |
validateStatusCode | Enables automatic status code validation. |
timeout | Sets a global timeout for all tests. |
reporters | Defines test output formats (e.g., cli, json, html). |
environment | Stores dynamic values (e.g., API keys). |
Let’s run a simple test against a /users endpoint.
Define a Test Case
Create a users.test.js file:
const { test, expect } = require('@noswag/core');
test('GET /users should return a list of users', async () => {
const response = await api.users.get(); // Auto-generated from OpenAPI
expect(response.status).toBe(200);
expect(response.body).toHaveLength(10); // Expect 10 users
});
Run the Test Execute the test using:
noswag test users.test.js
NoSwag supports custom assertions and conditional checks:
test('POST /users should create a new user', async () => {
const newUser = { name: "John Doe", email: "john@example.com" };
const response = await api.users.post(newUser);
expect(response.status).toBe(201);
expect(response.body).toMatchObject({
id: expect.any(String), // Check for a valid ID
name: "John Doe",
});
});
Instead of hardcoding API keys, use environment variables:
API_TOKEN=your_token noswag test
Group tests logically (e.g., users.test.js, products.test.js).
NoSwag automatically generates test stubs from your OpenAPI file, reducing manual work.
Run NoSwag tests in your pipeline:
# GitHub Actions Example
- name: Run NoSwag Tests
run: noswag test
If certain endpoints are unreliable, mock them:
test('GET /orders with mock', async () => {
api.mock('/orders', { status: 200, body: [] });
const response = await api.orders.get();
expect(response.body).toEqual([]);
});
NoSwag simplifies API testing by leveraging OpenAPI specifications, reducing boilerplate, and providing a clean, modern testing experience. By following this guide, you now have a solid foundation to: ✅ Install and configure NoSwag ✅ Write and run API tests ✅ Integrate testing into your workflow
Ready to take your API testing to the next level? Start experimenting with NoSwag today and share your experiences in the comments! 🚀
Happy Testing!
Guide to managing organizational change around API initiatives, including change management frameworks, communication strategies, and adoption metrics.
Strategic approach for CEOs to invest in quality initiatives and build a quality-first organizational culture, including cultural transformation, investment prioritization, and organizational excellence.
Guide to API testing side projects and personal development, including project ideas, skill building, and portfolio enhancement strategies.
Guide to managing organizational change around API initiatives, including change management frameworks, communication strategies, and adoption metrics.
Strategic approach for CEOs to invest in quality initiatives and build a quality-first organizational culture, including cultural transformation, investment prioritization, and organizational excellence.
Guide to API testing side projects and personal development, including project ideas, skill building, and portfolio enhancement strategies.
Comprehensive guide to API testing interview preparation, including common questions, technical assessments, and interview success strategies.