API Testing with Karate Framework

Burcu Akkaya
Insider Engineering
4 min readApr 10, 2023

--

Hi Everyone,

In this article, we’ll get to know API Testing with Karate Framework and go over the sample project.

With the popularity of BDD (Behaviour Driven Development), using the gherkin style in the automation project makes sense because offering development that everyone can understand has many benefits and provides a quick process for the team. Let’s explain the benefits of this by comparing Karate with Rest Assured Framework.

Comparison of Karate with Rest-Assured Framework

Firstly I started to experience Rest-Assured Framework for it but after exploring the Karate Framework it happened to be my favorite one. Because building a structure is very important to create a project that is suitable for growth. Most of the time it’s a cost for the team to understand and build the project. Unlike Rest-Assured, The utilization of Karate has proven to be cost-effective. Karate already creates ready-to-use gherkin steps for almost every action with API. We can think of it as a collaborative project for each different API project.

I won’t go into much detail about Rest-Assured but if you want to compare them, I’ll be adding projects for the same API created with Karate and Rest-Assured.

After comparing these frameworks in general, Let’s look at how we can use the Karate framework.

Set up Environment

Karate Framework supports JDK 1.8 and higher, so I use JDK 11.

You can find below the pom.xml example. I used ‘maven-archetype-quickstart’ when I started my project. You can select this archetype from IDE.

Creation of the new project from IntellijIDEA

The file has 3 dependencies, one of them is JUnit which I added to manage parallel execution for test cases, and others are used for Karate.

pom.xml example for the sample project

As a second action, to manage environment configuration and the other ones such as base URL, API key, etc., we should create a karate-config.js file into the src/test/java path. Be sure this file is in the correct path otherwise, you can not use the configuration that into the karate-config.js

karate-config.js

Now we’ve completed everything we need to start writing a script!

Let’s look at the examples with GET/POST/UPDATE/DELETE requests with Karate. After creating a sample feature file into the src/test/java path, you can write example requests like below.

There are the most basic GET and POST request examples. If you write a baseUrl, it reads from the karate config file automatically.

And also, you can verify the response with several types of match methods. (exact match, contains, present, etc.)

As you can see above, you can define the body payload in the scenario on the feature file and also you can create a JSON file and read it in the feature file.

On the other hand, Karate supports getting value from created java classes into the feature file. Compared with Rest Assured Framework, this feature makes the best difference in readability. You can find an example related to this.

pet.json
Example of the post request

Lastly, I’m adding the example of the delete and update requests.

Example of the post request

Configuration Parallel Testing

Configuration parallel testing is quite easy. We only need the feature file path and JUnit that we added already into the pom.xml.

You can configure how many test cases should be executed simultaneously.

The parallel() method manages how many cases are running at the same time. I preferred 4 cases for my project but we can increase the amount. It doesn’t have any limit.

TestRunner.java

Summary Report

Karate Framework generates the summary report automatically after running the test case. When all case execution is completed, the HTML report URL is created as follows.

HTML report generated after the execution
Sample Karate Test Report

Other Features and References

Karate Framework not only supports JSON as a format but also works on SOAP requests as XML. To give a wide assertion method structure, provide readable and ready-to-use request & response methods, and also retry configurations, this framework meets all the developer’s expectations.

I’m really glad to share my own experience with Karate. If you want also to use Karate for API testing, you can benefit from the main documentation from KarateLabs and also my example projects. Please feel free to ask me anything about it.

Finally, I’m pleased to be a part of the Insider and very excited about the work we will do in the future. If you would like to learn how we manage QA Process at Insider, you can read this article.

And also If you would like to be a part of this team, please do not hesitate to apply for our open positions. Join us!

Thanks!

--

--