API Testing with Keploy 🐇: From Event to Implementation
Introduction
As developers, we're always on the lookout for tools and techniques that can streamline our workflow and improve our code quality. Recently, I had the opportunity to attend a great event, "Deep Dive into APIs with Keploy". In this blog post, I'll share my experience and how I applied what I learned to my existing microservice project.
What’s Keploy???
I better quote it from the repo itself,
Keploy is developer-centric API testing tool that creates tests along with built-in-mocks, faster than unit tests.
Keploy not only records API calls, but also records database calls and replays them during testing, making it easy to use, powerful, and extensible.
I had written jest tests manually for each source file before :(. So, I was intriguied when I saw keploy. Another thing which piqued my interest is keployV2 doesn’t use SDKs for interacting, because it directly captures API calls at kernel level. This is a big plus for jugglers like me who frequently migrates his backend to different languages.
Deep Dive into APIs with Keploy
The event, hosted by Google Developer Student Clubs IIIT Kalyani , was part of the global Hacktoberfest celebrations. Here's a quick rundown of what made this session invaluable:
API Fundamentals: We delved into the core concepts that form the backbone of modern web applications and services.
API Testing: The session covered best practices and tools for effective API testing, a crucial skill in today's interconnected digital landscape.
Keploy: We got a firsthand look at how Keploy simplifies API testing and mocking, making it an essential tool for developers.
Putting Knowledge into Practice: Implementing Keploy in a Microservice
All hyped up, I decided to implement Keploy in one of my existing microservice projects. The project in question is a service that captures the current state of a website as a base64-encoded image using Puppeteer. I made this last year for a personal usecase, it’s written in Express JS.
Here's a brief overview of the microservice:
It's built with Express.js and uses Puppeteer to capture website screenshots.
The service exposes a POST endpoint
/fetch-screen
that accepts a URL in the request body.It launches a headless Chrome instance, navigates to the provided URL, and captures a screenshot.
The screenshot is then sent back to the client as a base64-encoded image.
Pretty simple… The repo if you wanna sniff repo
To integrate Keploy into this project, I followed these steps:
- Installing Keploy: I just followed the instructions at docs. It was pretty straightforward. Abhishek Kushwaha covered the installation part neatly on the session. It does require sudo access in linux since it captures network calls, but we can create a new usergroup called Keploy and add our user to it, to skip entering sudo everytime. You know, the way we work with Docker on linux :)
Recording API Interactions: I used the following command on the project root dir to record API interactions:
keploy record -c "pnpm start"
This command starts the application and records all incoming HTTP requests and their corresponding responses. The keploy-cli generates the config file, test and mock files for us which is under the keploy directory.
Running Tests: After recording, I ran the tests using:
keploy test -c "pnpm start" --delay 5
This command replays the recorded API calls and compares the responses with the recorded ones.
My thoughts…
Implementing Keploy in my microservice project, I could definitely see a lot of use-cases for keploy on my future works
Automated Testing: Keploy automatically generated test cases based on real API interactions, saving time and reducing the chances of missing edge cases.
Easy Debugging: The recorded API interactions make it easier to reproduce and debug issues.
Performance Insights: Keploy provides valuable insights into API performance, helping identify bottlenecks.
No AI Slop: No AI sloppy unit testing here.
Conclusion
For developers working with APIs, I highly recommend exploring Keploy. It's a testing tool for more robust, reliable, and efficient API development.
❤️ Open source
Thanks to Abhishek Kushwaha , keploy , Google Developer Student Clubs IIIT Kalyani
Subscribe to my newsletter
Read articles from Lovelin Dhoni directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by