API Testing made easy with Keploy

Hardik kumar
5 min readAug 4, 2022

Keploy is an API Automation Testing tool that uses Record and Replay method to test the APIs again and again.

In this blog, we’ll see how to use Keploy and replace the old ways of API testing and move forward with the automated way of testing them.

As we’re going to do it practically, we would be required to install a few software to get started.

….

To get started, we’ll use the same API that we’ve built in our API Creation in Go & Gin blog.

To connect our code with Keploy, we need to add a few lines of code in the main.go file. We will import Keploy and kgin

import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/keploy/go-sdk/integrations/kgin/v1" // NEW LINE
"github.com/keploy/go-sdk/keploy" // NEW LINE
)

Now change the func main() to the below code

func main() {
// Keploy configurations
port := "8080"
keploy := keploy.New(keploy.Config{
App: keploy.AppConfig{
Name: "superhero-api",
Port: port,
},
Server: keploy.ServerConfig{
URL: "http://localhost:8081/api",
},
})
router := gin.Default()
kgin.GinV1(keploy, router)
// Endpoints
router.GET("/", home)
router.GET("/superheroes", getSuperheroes)
router.POST("/superheroes", addSuperhero)
router.PUT("/superheroes/:id", editSuperhero)
router.DELETE("/superheroes/:id", removeSuperhero)
// router.Run(":" + port)
router.Run(":8080")
}

That’s it for connecting Keploy with our API.

Here, we’ll be creating a new file main_test.go in the same directory where main.go is available adding the below code in it. This file will be used later when we’ll be running our test cases (the explanation is available further in this blog)

package mainimport (
"github.com/keploy/go-sdk/keploy"
"testing"
)
func TestKeploy(t *testing.T) {
keploy.SetTestMode()
go main()
keploy.AssertTests(t)
}

To check if our API is successfully connected to Keploy, open the terminal and run the command go get . then go run . Re-check it at http://localhost:8080/superheroes

Next, we’ll open Docker and clone the Keploy repository

git clone https://github.com/keploy/keploy.git 
cd .\keploy\
docker-compose up

Once the Keploy server is up and running, we will start our application server over localhost at http://localhost:8081/testlist. A similar screen will appear like the screenshot below.

Keploy local server screenshot

Now, we’ll test our APIs in Thunder Client as we did in our last blog…

API Testing in Thunder Client

  • Goto Thunder Client
  • Create a new request, paste the link http://localhost:8080/superheroes and send the request

We are getting Status: 200 OK which indicates that the request has succeeded. And we are getting the data as expected (our initialized data).

Similarly, let’s do a POST request to add a new superhero. 201 Created is our goal here.

Now, let’s do the PUT and DELETE function too.

Once we complete these request, we’ll see 4 new test cases at http://localhost:8081/testlist.

Now we close our application with ctrl + C in the terminal.

If you remember, we have created a file named main_test.go . Let’s head over to that file and run it by clicking on the run button appearing on the left of TestKeploy() function.

Once we click on it, Keploy will test the application with the test cases and generate instant reports and alerts which will look like this

Here we can see that out of 4 tests, GET and POST requests are passed but PUT and DELETE requests failed. It’s becuase this was the very first time we’re checking our APIs and Keploy don’t have any previous data to compare it with.

Without changing anything in the code, let’s again run our test cases and see what happens.

This time, all the tests are passed as expected. We can click on the test to see theexpected output and the actual output side-by-side.

So this is how Keploy works. Easy, isn’t?

Next, we’ll directly just into the 3 value additions by Keploy.

3 Value additions by Keploy

1. Detailed Testing Coverage Reports

Keploy will test the new version of application with thousands of test cases within minutes and generate instant reports and alerts so that you don’t miss on anything!

2. Auto-Mocked Dependencies

Keploy understands the exact API call and the correct response to reply with, including dynamic elements. So if you’re using any external service like Databases, third party vendors, cloud services, etc Keploy will automatically mock their response hence eliminating need for external dependencies. Cut down on flaky tests with stable, repeatable environments.

3 . Record & Replay

Keploy automatically compares test-cases generated from previously collected traffic against updated behavior of your application, and brings any differences to your attention.

--

--