No Code API testing
Table Of Content
- No-Code API Testing with Keploy
- What is Keploy?
- How Keploy Works
- Time-Saving Benefits
- Keploy Installation
- Platform-Specific Requirements
- Quick Installation Using CLI
- Docker Installation
- Steps for macOS
- Steps for WSL/Linux AMD
- Docker Desktop for WSL 2
- Other Installation Methods
- Using Arkade
- Real-World Integration: Testing a Next.js CRUD Application
- Setting Up the Next.js Project
- Step 1: Initialize a Next.js Application
- Step 2: Install Required Dependencies
- Step 3: Set Up a Backend API
- Step 4: Add Environment Variables
- Step 5: Start the Development Server
- Integrating Keploy with the Next.js Application
- Step 1: Record API Calls
- Step 2: Replay Test Cases
- Step 3: Analyze Results
- How Keploy Saves Time and Effort
- Without Keploy:
- With Keploy:
- Keploy Unit Test Generator
- Usage
- Prerequisites
- Running with JavaScript/TypeScript Applications
- Generating Unit Tests
- Example
- Running with Golang Applications
- Generating Unit Tests
- Example
- Conclusion
- Key Links
No-Code API Testing with Keploy
In today’s fast-paced development landscape, the ability to test APIs without writing extensive code can save significant time and effort. Keploy is a no-code API testing platform that simplifies the testing process by automatically generating test cases and mocks. Let’s dive into how Keploy works, its benefits, and how to integrate it into your projects.
What is Keploy?
Keploy is an open-source tool designed to make API testing effortless. It uses eBPF to intercept API calls at the network layer and automatically generates test cases and mocks. This ensures comprehensive test coverage without the need for manual coding.
How Keploy Works
- API Call Interception: Keploy captures API calls and responses in real-time.
- Test Case Generation: It converts these interactions into test cases, including mocks and stubs.
- Regression Testing: Keploy replays the captured test cases to detect regressions in your API.
Time-Saving Benefits
- No Manual Test Writing: Keploy automates the creation of test cases, saving hours of manual effort.
- Faster Debugging: Identifying regressions becomes quicker with automated testing.
- Seamless Integration: Works with modern applications and frameworks, ensuring minimal setup time.
Keploy Installation
Platform-Specific Requirements
Below is a summary of the tools needed for both native and Docker installations of Keploy on different platforms:
Operating System | Without Docker | Docker Installation | Prerequisites |
---|---|---|---|
🍎 MacOS | ❌ | ✔️ |
|
🪟 Windows | ✔️ | ✔️ |
|
🐧 Linux | ✔️ | ✔️ | Linux kernel 5.15 or higher |
Quick Installation Using CLI
To get started, set up the Keploy alias using this command:
curl --silent -O -L https://keploy.io/install.sh && source install.sh
Once installed, you should see an output similar to the following:
🎉 Congratulations! You’re all set to use Keploy.
Docker Installation
For macOS and Windows, use Docker as native eBPF support is unavailable.
Steps for macOS
-
Open a terminal.
-
Create a bridge network in Docker:
docker network create keploy-network
-
Run the Keploy container:
alias keploy="docker run --name keploy-v2 -p 16789:16789 --network keploy-network --privileged --pid=host -v $(pwd):$(pwd) -w $(pwd) -v /sys/fs/cgroup:/sys/fs/cgroup -v /sys/kernel/debug:/sys/kernel/debug -v /sys/bpf:/sys/bpf -v /var/run/docker.sock:/var/run/docker.sock --rm ghcr.io/keploy/keploy"
Steps for WSL/Linux AMD
-
Open a terminal session.
-
Download and install Keploy:
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz --overwrite -C /tmp sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin/keploy
Docker Desktop for WSL 2
- Install Docker Desktop for Windows here.
- Configure Docker Desktop to allow WSL 2 distributions to access the Docker daemon. Refer to the official Docker documentation.
Other Installation Methods
Using Arkade
-
Install Arkade:
curl -sLS https://get.arkade.dev | sudo sh
-
Install Keploy:
arkade get keploy
-
To download a specific version:
arkade get keploy@<version>
Real-World Integration: Testing a Next.js CRUD Application
Setting Up the Next.js Project
To demonstrate Keploy’s capabilities, we’ll use a Next.js application as an example. This project will implement a simple CRUD (Create, Read, Update, Delete) functionality.
Step 1: Initialize a Next.js Application
Run the following command to create a new Next.js project:
npx create-next-app@latest keploy-crud-app
Follow the prompts to set up your application. Once complete, navigate to the project directory:
cd keploy-crud-app
Step 2: Install Required Dependencies
Install the necessary dependencies for building a CRUD application:
npm install axios mongoose
Step 3: Set Up a Backend API
Create a pages/api
folder and add CRUD endpoints. For example, to handle a users
resource:
// pages/api/users.js
import mongoose from 'mongoose';
const UserSchema = new mongoose.Schema({
name: String,
email: String,
});
const User = mongoose.models.User || mongoose.model('User', UserSchema);
export default async function handler(req, res) {
mongoose.connect(process.env.MONGODB_URI);
if (req.method === 'GET') {
const users = await User.find();
res.status(200).json(users);
} else if (req.method === 'POST') {
const user = new User(req.body);
await user.save();
res.status(201).json(user);
} else {
res.status(405).end();
}
}
Step 4: Add Environment Variables
Create a .env.local
file in the root directory and add your MongoDB URI:
MONGODB_URI=mongodb+srv://<username>:<password>@cluster.mongodb.net/<dbname>
Step 5: Start the Development Server
Run the following command to start your application:
npm run dev
Your CRUD API is now live at http://localhost:3000/api/users
.
Integrating Keploy with the Next.js Application
Step 1: Record API Calls
Run your Next.js application with Keploy to start capturing API interactions. Use the following command:
keploy record -c "npm run dev"
Step 2: Replay Test Cases
Once the API calls are recorded, you can replay them to test for regressions. Use this command:
keploy test -c "npm run dev" --delay 10
Step 3: Analyze Results
Keploy will provide detailed test case results, highlighting any regressions introduced by code changes.
How Keploy Saves Time and Effort
Without Keploy:
- Manual test case writing is required for each API endpoint.
- Developers must handle mock data and test configurations manually.
- Testing for regressions involves repetitive effort.
With Keploy:
- Automatically generates test cases from real API interactions.
- Simplifies mock and stub management.
- Enables quick and efficient regression testing.
This automation reduces development overhead and ensures reliable API performance.
Keploy Unit Test Generator
Keploy's unit test generator (UTG) implementation of the Meta LLM research paper is the first to understand code semantics and generate meaningful unit tests. It aims to:
- Automate unit test generation (UTG): Quickly generate comprehensive unit tests and reduce redundant manual effort.
- Improve edge cases: Extend and improve the scope of tests to cover more complex scenarios that are often missed manually.
- Boost test coverage: As the codebase grows, ensuring exhaustive coverage becomes feasible.
Usage
keploy gen [flag]
Prerequisites
An API key for the AI model is needed, which can be obtained from:
- OpenAI's GPT-4 (preferred)
- Alternative LLMs via litellm
- Azure OpenAI Services
Set the API key as an environment variable:
export API_KEY=xxxx
Running with JavaScript/TypeScript Applications
To ensure the coverage report is in Cobertura format, modify your package.json
:
"jest": {
"coverageReporters": ["text", "cobertura"]
}
Or, if jest.config.js
is present:
module.exports = {
coverageReporters: ["text", "cobertura"],
};
Generating Unit Tests
You can test a smaller section of the application or control costs by generating tests for a single source and its corresponding test file:
keploy gen --source-file-path="<path to source file>" \
--test-file-path="<path to test file for above source file>" \
--test-command="npm test" \
--coverage-report-path="<path to coverage.xml>"
For the entire application, use --test-dir
instead of --test-file-path
.
⚠️ Warning: Running the command with
--test-dir
will generate unit tests for all files in the application. Depending on the size of the codebase, this process may take between 20 minutes to an hour and will incur costs related to LLM usage.
Example
Consider an Express-Mongoose sample application with Jest test cases under the test
folder named routes.test.js
. Modify the package.json
:
"jest": {
"collectCoverage": true,
"coverageReporters": ["text", "cobertura"],
"coverageDirectory": "./coverage"
}
Run the Keploy UTG command:
keploy gen \
--source-file-path="./src/routes/routes.js" \
--test-file-path="./test/routes.test.js" \
--test-command="npm test" \
--coverage-report-path="./coverage/cobertura-coverage.xml"
We will get the following output:
Keploy test coverage with AI-generated unit tests for Express-Mongoose
Voila!! The generated test cases provide 58% coverage🌟
Running with Golang Applications
To ensure Cobertura-formatted coverage reports, install:
go install github.com/axw/gocov/gocov@v1.1.0
go install github.com/AlekSi/gocov-xml@v1.1.0
Generating Unit Tests
Run the following command:
keploy gen --source-file-path="<path to source file>" \
--test-file-path="<path to test file for above source file>" \
--test-command="go test -v ./... -coverprofile=coverage.out && gocov convert coverage.out | gocov-xml > coverage.xml" \
--coverage-report-path="<path to coverage.xml>"
For the entire application, use --test-dir
instead of --test-file-path
.
⚠️ Warning: Running the command with
--test-dir
will generate unit tests for all files in the application. Depending on the size of the codebase, this process may take between 20 minutes to an hour and will incur costs related to LLM usage.
Example
Consider a Mux-SQL sample application with a test file app_test.go
for the source file app.go
:
keploy gen --source-file-path="app.go" \
--test-file-path="app_test.go" \
--test-command="go test -v ./... -coverprofile=coverage.out && gocov convert coverage.out | gocov-xml > coverage.xml" \
--coverage-report-path="./coverage.xml"
Conclusion
Keploy is a game-changing tool for developers looking to streamline API testing and unit test generation. By automating the creation of test cases, intercepting real-world API calls, and generating comprehensive coverage reports, Keploy dramatically reduces the manual overhead traditionally associated with software testing.
With tools like Keploy UTG, developers can achieve:
- Higher test coverage with less effort.
- Improved test quality by covering edge cases that are often missed.
- A scalable, cost-effective testing process, regardless of codebase size.
Key Links
By integrating Keploy into your development workflow, you can ensure robust API performance, reduce debugging time, and deliver high-quality applications faster.