API Testing with Karate framework


 API Testing with Karate framework


Life has changed suddenly with all of us having to work continuously from home due to the Covid-19 epidemic. One of the ways I found to keep myself motivated is to learn new technologies and tools and also to brush up on my existing knowledge. 

Today I will talk about Karate. No, it's not about the sport Karate, but it's about the Karate framework which I learned while working from home. Karate is an open-source API testing tool which helps with testing REST services API services. Karate is based on BDD style and is similar to how we would write Cucumber we don't need to write step definitions. 

In this blog post I will give some basic steps I followed to set up a maven project and invoke a test endpoint.

Once we have created a maven project using our favorite ide such as IntelliJ we need to add the required dependencies to the pom.xml. When I played around with karate API the dependencies I added are as below: Note that karate-junit4 dependency will help to facilitate JUnit testing for our example.

<?xml version="1.0" encoding="UTF-8"?>
<dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.1.0</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>com.intuit.karate</groupId>
      <artifactId>karate-apache</artifactId>
      <version>0.9.5</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>com.intuit.karate</groupId>
      <artifactId>karate-junit4</artifactId>
      <version>0.9.5</version>
      <scope>test</scope>
   </dependency>
</dependencies>
 
Also, update the pom.xml with the maven-surefire-plugin as below

  <?xml version="1.0" encoding="UTF-8"?>
<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>2.22.2</version>
      </plugin>
   </plugins>
</build>
 
Once the above dependencies are added we need to start writing our feature file. For that, I created a package called feature and added the feature file as below.

Feature: get employee details Background:
  * url 'http://your-url.com'
  * header Accept = 'application/json' 
Scenario: get all employee details
   Given path '[Your-Resource-Path] ex:/api/v1/employees'.
  When method get
 
Then status 200
 
And print 'Response is: ', response
 
And match response contains {"data":[{"id":"001","employee_name":"Shavantha"}]
 
From the above code, the first line is Feature. Here we write the feature which we want to test in simple English.

Next, under Background, we have to give the base URL which has the API endpoint which we want to call and the header parameter that we are passing.

The Scenario section is the next most important section. Under Scenario, the next most important keyword we need to add is Given. This is where we specify the resource path which we want to call.

When a keyword is used in this scenario to specify that the API call is of type GET and the Then keyword is used to specify that we are expecting a response type of 200 for the successful invocation of the API Get a call.

When testing API endpoints one of the most important aspects is to assert if the response is ax expected, with Karate we can achieve this with the keyword match. As shown below we are asserting that the response contains the employee ID  and Name parameters.


And match response contains {"data":[{"id":"001","employee_name":"Shavantha"}]

If we need to match if a response value is not null we can achieve that as  [{id:"#notnull", this way we can assert if a required value is returned from the GET call.


The Java source file as shown below we are calling the @RunWith annotation and is passing Karate.class as the parameter.  This class is added so that the test will be recognized by the JUnit runner.

package karate;

import com.intuit.karate.junit4.Karate;
import org.junit.runner.RunWith;

@RunWith(Karate.class)
public class APITest {
}
One final point for this article before I wrap up is that most of the time access to API is secured and needs to access through an authorization key. This can be achieved by passing header Authorization = 'Bearer <key>' parameter under the Background keyword'

Based on the steps given above I can see that the Karate framework provides an easy and simple approach for developers and testers to test the API endpoints.


 

Comments