Getting Started with Keploy
Tags: keploy, webdev, javascript, beginners
Published: 2023-10-12 17:26:48

## Introduction

This is the age of "Automation". And We, Developers, always try to automate boring tasks!

One of those boring tasks is Testing APIs by making API calls and checking if the response is right or not. But Keploy Solves this Problem pretty well. So in this article, we'll explore what is Keploy, How it works, and how to set it up locally with examples!

So without delaying further, Let's Start!

## What is Keploy?

![keploy logo](https://camo.githubusercontent.com/703f03885b2eb91fb5ad2105584d96f189d42e6ed80b59a1ad6802ebde22b0ab/68747470733a2f2f646f63732e6b65706c6f792e696f2f696d672f6b65706c6f792d6c6f676f2d6461726b2e7376673f733d32303026763d34)

Keploy is an Open Source API testing platform to generate Test cases out of data calls along with data mocks.

In simple words, With Keploy we don't have to write manual test cases. It records API interactions and expected responses and generates test cases and data mocks to make our work easy and efficient.

## Why Keploy?

Before understanding Why we need Keploy, First, understand the problem!

Suppose, We are going to create a complex application. But as the complexity and codebase increases, It's very hard for us to manually write test cases and manage them. It's like a never-ending marathon!

Here comes Keploy! It automates the process of generating test cases and makes it easier for the maintainers to maintain the test cases efficiently. So we can focus on more critical works than writing those bearing test cases.

## How keploy works?

So Till now, we know what is keploy and why we need keploy! Now Let's understand how keploy works!

![Test Case Generator](https://keploy.io/docs/gif/record-tc.gif)

Whenever our application makes an API call, the keploy SDK records the request subsequent network calls to external dependencies, and the expected response. Internally It combines all the information and stores it as a test case in the server.

Now, here's the cool part.

Next time when we'll be working on the new version of that project, Keploy will automatically download the previously captured test cases and run them alongside them.

Not only that Keploy will also compare the actual response with the expected one and throw errors if we have any bugs.

## Setting up Keploy Locally

In this Section, We'll set up Keploy locally and generate test cases for a Sample Nodejs application.

Sound Interesting? Right?

First things first, We'll be cloning one sample application from GitHub and move to the `samples-typescript/express-mongoose` Folder. For that write this in your WSL:

```bash
git clone https://github.com/keploy/samples-typescript && cd samples-typescript/express-mongoose
```

Now, We'll install all the required packages using `npm install`.

For this project, we'll be using Keploy Natively on WSL.

On Windows, WSL is required to run Keploy Binary. For that, we need Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11 to use the commands below.

Now, We'll download and Install "Keploy Binary". For that use the following command in your terminal:

```bash
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp

sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy
```

This command downloads and installs the keploy from the above-mentioned URL, extracts its contents, and stores them in the `/tmp` directory.

In the Next line, It moves the keploy from `/tmp` directory to `/usr/local/bin` **directory.** This makes the `keploy` executable & available system-wide for all users.

After Running this command we'll get something like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696710929506/1ba7d8f3-21a0-40b2-8383-3bf34a7e11dd.png)

So We have natively installed Keploy!

Pretty Simple! Right?

So let's Move to the next part!

We'll now start our **MongoDB** instance!

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong>NOTE: As we are setting up our sample app natively, we need to update the MongoDB host on line 4, in </strong><code>db/connection.js</code>, from <code>mongodb://mongoDb:27017/keploy</code> to <code>mongodb://127.0.0.1:27017/keploy</code>.</div>
</div>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696711960405/882c1078-7ff2-4680-acba-e8826ec0d4d8.png)

> To start MongoDB Instance, we'll be using Docker. Make Sure you have Docker installed in your system!

Here's the command to start our MongoDB :

```bash
docker-compose up -d
```

This command will start Docker containers defined in the Docker Compose configuration file. Here's the Docker-compose file of our project.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696711395162/0ab60d63-f81b-48e5-b265-64b500628d37.png)

Now after running the command, we'll get :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696711613291/fe4df465-d538-40e2-bfa6-b2f61c3cdd67.png)

Okay! So we got some errors!

> To fix this, go to your Docker Desktop Settings the change the following:
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696711771921/395ee638-51c6-4780-8606-75dceadf6afc.png)

Now it will work properly!

But Here we can get one more error, it will show this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696740830720/6c2d2b3a-bb86-425f-b3fe-63b7e6a50b8c.png)

Don't worry much! Let's fix this together!

So We are getting this error because Docker Compose cannot find keploy-network in our Docker setup!

To check if the "keploy-network" actually exists as an external network in your Docker setup. Run the following command:

```bash
docker network ls
```

And We Got this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696741711870/3c328341-91d2-4ad5-8ecf-060108fb0570.png)

That means we don't have the "keploy-network" as an external network!

To fix that, we'll create a docker network using the following command:

```bash
docker network create keploy-network
```

We'll get one network ID in response like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696742134799/abeae919-0d5f-47b8-a57e-0a5c9c135dd3.png)

Now if we try to run the previous command it will work.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696742292824/1deb48fc-2299-4283-827d-da7f9812a8f4.png)

After that in the MongoDB Compass we'll get something like this;

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696712091083/d64993ba-e93f-4040-8491-e47cb137b2ce.png)

After that, We'll capture the test cases with keploy. Here's the command for that:

```bash
sudo -E env PATH="$PATH" keploy record -c 'npm start src/app.js'
```

We'll get something like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696742865157/983db152-600b-4832-88ab-a2db8bba2895.png)

After connecting to MongoDB, We'll send a post request in Postman!

We can also make a Post request by running this command :

```bash
curl --request POST \
--url http://localhost:8000/students \
   --header 'content-type: application/json' \
   --data '{
    "name":"Arindm Majumder",
    "email":"arindam@gmail.com",
    "phone":"2902357012"
    }'
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696742998772/1394a2c8-0aa5-46ec-b69a-763637092b93.png)

Great! Now in Terminal, we can see Keploy has captured the test cases.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696743137945/36eed220-bbf1-4b16-9acc-8cde99efdacb.png)

In the MongoDB Dashboard, we can also see the data

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696743424327/b3eee8fa-de5d-4df3-8cb8-e288566b2ebb.png)

And Now, In our project, we can see test cases have been generated in the `keploy` folder.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696743481456/621dcdb2-8818-42c5-92a4-3cc1ae48d316.png)

Great work! We have successfully generated the test cases! Now we can test our APIs with ease!

## **Celebrate Hacktoberfest with Keploy**

![Hacktoberfest2023](https://keploy.io/docs/assets/images/hacktoberfest-2023-5f363291b76065eb9a1de65b5de0aadc.png)

October is the month of Open Source and Keploy is taking part in this celebration. You can contribute to several [Keploy](https://github.com/keploy/keploy) projects by participating in this year’s Hacktoberfest. You can both contribute to the code part and the no-code part as well. Here are some contributions that you can make!

#### **Code Contributribution includes:**

* 🛠️ Bug fixes
    
* 👉 New features
    
* 👨‍💻 Build Sample Apps
    

#### **Non-code contributions include:**

* Documentation
    
* Create a Tutorial
    
* Blog writing
    
* Translation
    

So what are you Waiting for?

Give a Star, Fork the Repository of [Keploy](https://github.com/keploy/keploy) and Make Your First Contribution!

## Conclusion

If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more content on Javascript, React, and other web development topics.

To sponsor my work, please visit: [Arindam's Sponsor Page](https://arindam1729.hashnode.dev/sponsor) and explore the various sponsorship options.

Connect with me on [Twitter](https://twitter.com/intent/follow?screen_name=Arindam_1729), [LinkedIn](https://www.linkedin.com/in/arindam2004/), [Youtube](https://www.youtube.com/channel/@Arindam_1729) and [GitHub](https://github.com/Arindam200).

Thank you for Reading :)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696769304787/4a3802f8-8646-48af-a3c6-151b8ad07add.png)