Lets Do API Testing With Karate Framework

Reading Time: 3 minutes

API Testing

Application Programming Interface(API) is nothing and it is a logic that is written by the developer and as a tester you will testing it. tester verifying that it is meet the expected result or not it is called API Testing.API testing is a software testing that tests the APIs  from their functionality, reliability, performance, to security.

Karate (DSL) For API Testing

Initially Karate was created by ‘Peter Thomas’ In 2017.Karate is a open source tool for api testing(SOAP & REST).karate is written in java.we can do API mocking,UI testing,performance testing of the API’s using karate. karate uses BDD syntax. We also use the features and scenarios in karate.

Features of karate:-

  1. Easy for non programmers(written in java,but implemented using BDD style).
  2. Requires no technical programming language so you don’t worry if you have not any programming background.
  3. We can do both parallel testing and Distributed testing using karate.
  4. We can do multi environment testing and therefore we can set multiple environment and do our testing and we can get our data from external files (CSV, JSON files).

Types of API Request in API Testing

4. Programming Read/Write Services - RESTful .NET [Book]
  1. GET- Basically It used for the get the data from any resource.
  2. POST- It also used for the send the data to API.
  3. DELETE-While it is used for the delete the data from API.

Prerequisites for karate


Karate requires at least java 8

Also Karate works fine on openjdk


We need some platform where we write our test and run and check our result. we need an IDE that is an integrated development environment. Therefore We have multiple options(Eclipse IDE,Intellij,VSCode).


Also Install Cucumber plugin to create and run feature files

Karate project Structure

  1. Pom.xml file:-this is a file where we put all the dependencies and plugins to run the code.following are the dummy structure of the pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">






2.TestRunner.java file:-it used for the execute  test scenario which is present in the feature files.following is the structure of the TestRunner.java file.

package tests;
import com.intuit.karate.junit5.Karate;

class testrunner {
Karate testSample() {
return Karate.run("get").relativeTo(getClass());
Karate testSample2() {
return Karate.run("post").relativeTo(getClass());

3 .feature file:-It puts all the testing scenarios that are needed to be tested ,Therefore feature file should be present in the same folder in which TestRunner is present. Feature file is where we write our test. following are the structure of the feature files for get and post method.

Feature: Get API Demo

Scenario: Get Demo 1
Given url 'https://reqres.in/api/users/2'
When method GET
Then status 200
Then print response
Feature:Post API Demo

Scenario: Post Demo
Given url 'https://reqres.in/api/users'
When method POST
Then status 201
Then print response

Karate Configuration(Karate-config)in karate framework

Karate expects a file called karate-config.js to exist in the class path and contains a function JavaScript.This function will  return the JSON object. all keys and values for that JSON object will be available as  variables.If you want to set some common variables across all your feature files. or if you run your test case in multiple environment. or when you want to globally stored the authentication token generation data like client id,client secret in some particular file. then you can make use of karate-config.js. lets see an example

Create a file karate-config.js in src java package

function fn(){
var config = {
abc : 'test'
return config;

Create config.feature file

Feature: user Details
* def expectedOutput = read('../data/result2.json')

Scenario: request user Details
Given url 'https://reqres.in/api/users/2'
When method GET
Then status 200
And match response == expectedOutput
Then print 'abc---' ,abc




Written by 

I am a software consultant at knoldus Inc. I have completed my post-graduation from Uttrakhand university. I have some knowledge of core concepts of Manual and Automation Testing and tools such as Postman, Selenium, and Jmeter.