DEV Community

Cover image for Stop Writing Repetitive Tests: How Keploy Generates Them Automatically
Metta Surendhar
Metta Surendhar

Posted on

Stop Writing Repetitive Tests: How Keploy Generates Them Automatically

When I first started learning about APIs and CRUD operations, I tested everything manually. I would run the application, perform an action in the UI or call an API, and then check if it behaved correctly. At that point, I didn’t even know what unit testing or or API tests was.

Later, I realized that writing test cases is crucial. They help catch bugs early, reduce the risk of regressions, and give confidence when making changes. However, there was one big problem: writing tests felt repetitive and time-consuming. Instead of focusing on building features, I was spending hours writing boilerplate code for tests.

That’s when I came across Keploy, a tool that automatically records API calls and generates test cases and data mocks. This means you can test your application without writing traditional test scripts.


Why Automated Testing Matters

Before diving into code, let’s briefly revisit why automated tests — whether unit tests or API tests — are important:

  • Catch bugs early – Problems are detected before deployment.
  • Prevent regressions – Ensures that new changes don’t break existing features.
  • Improve confidence – Developers can refactor code without fear.
  • Save time in the long run – Manual testing becomes unnecessary for routine checks.

The only challenge? Writing and maintaining tests manually is tedious. This is where Keploy steps in.


What is Keploy?

Keploy is an open-source testing toolkit designed primarily for API testing. It helps you automate validation without writing traditional test scripts:

  • Records API calls and responses while you use your application.
  • Automatically generates test cases in YAML format.
  • Creates mocks for external dependencies (databases, third-party APIs).
  • Provides a simple test mode to replay requests and validate responses.

Think of Keploy as a record-and-replay testing tool. You interact with your app once, and Keploy creates reusable API-level tests for you.


Step 1: Build the To-Do List App

Before we dive into Keploy, let’s first build a simple To-Do List application. This app will serve as the foundation on which we’ll later generate automated test cases.

We’ll use:

  • Flask → A lightweight Python web framework.
  • SQLite → A simple database to store tasks.
  • Flask-SQLAlchemy → ORM (Object Relational Mapper) for managing database operations.
  • Jinja2 Templates → To render HTML pages.

The app is a classic CRUD (Create, Read, Update, Delete) example, which makes it a perfect candidate for learning automated testing.


Features

Our To-Do List app will support the following:

  • Add tasks: Enter a task and save it into the database.
  • View tasks: See all tasks with their creation time.
  • Update tasks: Edit an existing task’s content.
  • Delete tasks: Remove tasks you no longer need.
  • API endpoints: Access the same functionality programmatically with REST APIs.

This way, we’ll have both a web UI and a set of APIs for interaction.


Project Structure

D:.
├─ static
│  └─ css
├─ templates
├─ tests
└─ app.py
Enter fullscreen mode Exit fullscreen mode
  • app.py – Main Flask application containing:
    • Database model (Todo)
    • UI routes (for web pages)
    • API routes (for REST APIs)
  • templates/ – HTML templates used for rendering pages:
    • base.html → Shared layout for all pages.
    • index.html → Main page showing the task list.
    • update.html → Page for updating an existing task.
  • static/css/ – Custom CSS for styling the UI.
  • tests/ – Placeholder directory where automated test cases will be generated later using Keploy.

Database Model

Inside app.py, we’ll define a simple Todo model using SQLAlchemy.

  • id: Unique identifier for each task.
  • content: The actual task description.
  • date_created: Timestamp of when the task was added.

Task List UI

Once you start the app and add some tasks, the home page (index.html) will display them in a neat table:

  • Column 1 → Task description.
  • Column 2 → Date created.
  • Column 3 → Update/Delete actions.

At the bottom, there’s a form input to quickly add new tasks.

✅ At this point, we’ve built a complete Flask To-Do List app with both UI and APIs. This will now serve as the base project for integrating Keploy and generating test cases automatically.


Step 2: Run the Application

You can clone the project from [To-Do-List-Flask] and follow the README to initialize and run the app.

Install virtualenv:

$ pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

Open a terminal in the project root directory and run:

$ python -m venv .venv
Enter fullscreen mode Exit fullscreen mode

Then run the command:

$ .venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Then install the dependencies:

$ (.venv) pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Finally start the web server:

$ (env) python app.py
Enter fullscreen mode Exit fullscreen mode

This server will start on http://127.0.0.1:8080/ by default


Step 3: Install Keploy

If you’re on Windows, you’ll need WSL (Windows Subsystem for Linux).

Initialize wsl:

C:\Users\Dell> wsl
unix@DESKTOP:/mnt/c/Users/Dell$ 
Enter fullscreen mode Exit fullscreen mode

Install command:

$ curl --silent -O -L https://keploy.io/install.sh && source install.sh
Enter fullscreen mode Exit fullscreen mode

Check Installation:

$ keploy -v 
Enter fullscreen mode Exit fullscreen mode

👉 For Linux/macOS, follow the Keploy installation guide


Step 4: Record with Keploy:

Now comes the fun part. Keploy has a record mode that listens to API calls while you use your application.

Run:

keploy record -c "python app.py"
Enter fullscreen mode Exit fullscreen mode

Now, perform some actions through both the UI and APIs.


UI Actions Captured

  • Create a task

  • View tasks

  • Update a task


  • Delete a task

Terminal Output


API Actions Captured

  • Create a task
curl -X POST http://localhost:8080/api/tasks \
  -H "Content-Type: application/json" \
  -d '{"content":"blog demo task"}'
Enter fullscreen mode Exit fullscreen mode

  • Update a task
curl -X PUT http://localhost:8080/api/tasks/1 \
  -H "Content-Type: application/json" \
  -d '{"content":"updated task"}'
Enter fullscreen mode Exit fullscreen mode

  • View tasks
curl -X GET http://localhost:8080/api/task
Enter fullscreen mode Exit fullscreen mode

  • Delete a task
curl -X DELETE http://localhost:8080/api/tasks/1
Enter fullscreen mode Exit fullscreen mode

Terminal Output

Each of these interactions gets stored as a test case in YAML format inside the keploy-tests/ folder.


Step 5: Run Tests with Keploy

Once tests are recorded, run them anytime with:

$ keploy test -c "python app.py"
Enter fullscreen mode Exit fullscreen mode

Keploy will:

  • Replay all recorded requests.
  • Compare the actual responses with recorded ones.
  • Generate a detailed test report.

This ensures that your API behaves consistently over time.

Generated Test Cases:

Generated Reports:


Benefits of Using Keploy

Adopting Keploy in your development workflow brings several advantages:

  1. Automatic Test Generation

    • No need to manually write repetitive test cases.
    • Keploy records your real API traffic and generates test cases in YAML format.
  2. Keeps Tests Up-to-Date

    • Whenever your API changes, just record again.
    • Test cases evolve with your application, reducing maintenance overhead.
  3. Data Mocks for External Services

    • Keploy automatically generates mocks for databases, third-party APIs, or external dependencies.
    • This allows tests to run reliably without depending on live external systems.
  4. Language & Framework Agnostic

    • Works with popular backend frameworks like Flask, Django, FastAPI, Spring Boot, Express.js, and more.
    • Flexible enough to integrate into diverse tech stacks.
  5. End-to-End Coverage

    • Captures both UI-driven actions and API requests.
    • Provides comprehensive testing without extra setup.
  6. CI/CD Integration

    • Generated tests can be run in pipelines.
    • Ensures every deployment is validated with the same rigor as your local environment.

Conclusion

Automated testing is critical for building robust, bug-free applications, but writing and maintaining test cases can often feel repetitive and time-consuming. This is where Keploy changes the game.

By automatically recording real-world API interactions and generating tests with data mocks, Keploy ensures:

  • Your APIs behave consistently.
  • Tests evolve naturally with your application.
  • Development cycles become faster and more reliable.

In this blog, we built a Flask To-Do List app and saw how easily Keploy can record interactions, generate API test cases, and validate our endpoints. The key takeaway is:

👉 With Keploy, you record once and test forever.

If you’re working with Flask, Django, FastAPI, or any modern backend, I highly encourage you to give Keploy a try. It’s a huge productivity booster and ensures your applications remain reliable as they grow.

Top comments (0)