Automate API Testing with Karate

What is Karate?

Karate is an open-source tool for automated API testing. The tool was released by Intuit and the project can be found at https://github.com/intuit/karate

The main advantage of Karate over other automated API testing tools is that it requires virtually no knowledge of any programming language.

Karate’s API Testing DSL

In order to test a given API, you simply write your tests in plain text in Gherkin (Given, When, Then) format using the tool’s DSL.

As Karate sits on top of Cucumber it understands the concept of feature files. This is how the API testing scenarios are constructed, in a file with .feature extension.

In contrast to Cucumber however, you don’t need to write any step definitions. You interact directly with the API via Karate’s DSL.

Despite not needing much knowledge of programming languages to use Karate, that isn’t to say that a basic understanding of HTTP, JSON, XML, JsonPath and XPath and JavaScript won’t help.

How to use Karate

If you are using Maven, you need the two following dependencies

<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>0.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>0.6.0</version>
<scope>test</scope>
</dependency>

The Maven tradition is to have non-Java source files in a separate src/test/resources folder structure – but the creators of the Karate tool recommend that you keep them side-by-side with your *.java files.

Like Cucumber, you need to have a “Runner” class which runs the feature file(s). Unlike Cucumber, however, there are no step definitions! This is the magic of Karate.

To use the TestRunner.java class to execute the feature file, you need to have the build section in the pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Tutorials</groupId>
<artifactId>Karate</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>0.6.0.4</version>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>0.6.0.4</version>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
</build>
</project>

And your TestRunner.java class would look like this:

package com.tutorials.karate;

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

@RunWith(Karate.class)
public class TestRunner {
}

API Testing with Karate

Suppose you are testing an API which returns a list of users in JSON format:

[
{
"id": 1,
"name": "FirstUser",
"password": "User1Pass"
},
{
"id": 2,
"name": "SecondUser",
"password": "User2Pass"
}
]

Your Karate feature file will look like:

Feature: Test User API
Scenario: Fetch all users
Given url 'https://some-api.com/api/users'
When method GET
Then status 200
And assert response.length == 2
And match response[0].name == 'FirstUser'

As you can see, the test is concise, to the point and most importantly, no code! It uses the Karate’s DSL to interact with the API directly.

Karate comes with a rich set of useful features which enable you to perform Automated API Testing easily and quickly.

For more information see: https://github.com/intuit/karate

Leave a Reply

Your email address will not be published. Required fields are marked *