Karate a Rest Test Tool – Basic API Testing

Karate DSL API Testing

I recently learned about a new, open-source API testing tool called Karate, which helps with testing web and REST services API’s in Java. So I thought I’d enter the API testing dojo and check out how it works. Today’s post will focus on some Karate DSL basic techniques.

*I first learned about Karate in early 2017, when the tool was still in its early stages of development, and immediately saw its potential. Since then Karate as matured in a stable tool with unique functionality, such as having API Testing, API testing doubles, and API performance testing all available in one framework.

API tests are written using BDD Gherkin syntax. But unlike most BDD frameworks (Cucumber, JBehave, SpecFlow), you don’t need to write step definitions. Karate has already created all the step definitions you need to get started testing APIs.

If you’re new to programming or automation testing Karate is easy to use since no Java knowledge is required. If this is you, Karate might be the perfect choice for your team.

Karate API Test Tool Setup

Before we can check out some of the testing moves you can perform with Karate, we need to have a project set up.

The easiest way to do this is to create a new project based on the Karate Maven archetype using Maven. I’ll assume you already have Java and Maven installed. If not, check out my videos on How to Install Java and How to Install Maven.

Set Up your Karate REST Test Project

1. Start up your favorite IDE. (I’ll be using Eclipse for this example.)
2. Go to File>New>Maven Project and take the defaults on first screen.
3. Under New Maven Project, create a click on Add Archetype
4. Enter the following info:

Archetype Group Id= com.intuit.karate
ArchetypeArtifactId= karate-archetype
ArchetypeVersion=0.2.7
5. Click OK.
6. It should find the Karate-archetype. Click on Next.
7. On the next screen, enter:

GroupId=com.testtalks.karatedemo
ArtifactId=karatedemo

Existing Java CucumberJVM Maven Projects

If you have an existing CucumberJVM Maven project, all you need to do is add the following dependency in your pom.xml file:

com.intuit.karate
karate-junit4
0.2.7
test

Once you have your project set up, we can begin coding your REST test API in Java.

We’ll be using Jenkins as the test application. Jenkins comes with a nice REST API that we can easily interact with as an example to test JSON.

Create your First Rest API Test using Karate

1. Under src/test/java, create a new file called Jenkins.feature
2. Enter the following:

Feature: Demo Karate against Jenkins
Scenario: Verify that Jenkins is up and running
Given url 'http://localhost:8080/api/json?pretty=true'
When method get
Then status 200
And match response.jobs[*].name contains ['JoeProject1']

Click Run.

Your test should pass.

Karate Rest Test Tool – No Code Required!

What’s cool about Karate is that unlike most BDD frameworks (Cucumber, JBehave and SpecFlow), you don’t need to write step definitions. The reason for that is that Karate has already created all the step definitions you need in order to get started with JSON, HTTP or XML.

You don’t even need to write Java code! You can create your API test in a BDD syntax without the overhead of writing any code to implement your scenarios.

Rest API Test Tool Using CucumberJVM

Since Karate’s REST test tool is built on top of Cucumber-JVM, you can run your tests, view reports and leverage any other Cucumber functionality just as with any standard Java project.

In addition, the “Getting Started” guide for Karate recommends using jUnit — NOT TestNG — since things like dynamic tables, data-driven testing and tag groups are already built in.

Is Karate Better Than Rest-Assured?

I’m just getting started with Karate myself, but as you can see in the video I was able to navigate pretty easily with zero prior knowledge of it.  Its too early for me to really compare Karate vs Rest-Assured. I’m also a big believer in that there is not better tool its more what tool is better for your team. I know that the creator of Karate Peter has an awesome comparison of the two.  What do you think?

8comments
Richard Swedish - January 5, 2018

Joe,

We are about to start building out a API testing framework using Rest-Assured. We like Rest-Assured and it fits well into our eco-system.

But, I work for a large company and we still have a lot of SOAP Services and Rest-Assured does not work with SOAP (that I am aware of).

I like what I have read about Karate and that does not require coding (even though my team could do the coding) but other teams cannot.

So, my questions are:

1.) How reliable/mature is Karate now?
2.) It says it can work with REST and SOAP is that correct?
3.) Is there another OPEN SOURCE SOAP testing tool out there that we can run tests in the cloud using (as I don’t want to use SOAPUI as it requires client installs).

Please let me know. This is a big to do on our roadmap of testing tools to add this year.

Thanks as always Joe.

Reply
Raj - February 10, 2018

Hi
I am working on angular application . Would this be the right tool for it?

Reply
    Joe Colantonio - February 10, 2018

    HI Raj – what kind of application are you trying to test? My understanding is that Angular is a platform that makes it easy to build applications with the web. So if you want to test a web front end than NO an API testing tool like Karate would not be the right choice. If your application has APIs that are consumed by your Angular front end than yes you could use Karate to test those APIs

    Reply
Irfan - February 13, 2018

Hi Joe,

I noticed that the above article was posted almost a year ago so I agree to your comment the it is too early for you to compare Karate vs Rest-Assured.

But now would it be safe to ask your opinion on Karate vs Rest-Assured comparison?

Thanks
Irafan

Reply
    Joe Colantonio - April 24, 2018

    Irafan I think both are great options. I know that Karate has been steadily improving but I have not done any active programming with it. I need to check it out again and see :)

    Reply
Sean Jones - March 16, 2018

Hi Joe C.,
I’m currently working on Karate for a current project. For which I find Karate very intuitive. I’m running into a dilemma concerning POST json body validation with Karate. Could you refer me to some materials?

Thanks,

Reply
kushal - April 3, 2018

Hi,

Just wanted to know in Which Scenarios we can use Karate over Rest-Assured. or in your opinion so far is Karate Better than Rest-Assured?

Reply
    Joe Colantonio - April 24, 2018

    Not sure one is “better” over the other. Both are great options. I would recommend trying both and have your team decided which is best for them.

    Reply
Click here to add a comment

Leave a comment: