Niron Perera

Kick off web services testing with KARATE

Karate is a test automation framework created to make your web services testing simple.

Karate is a test automation framework created to make your web services testing simple. If you are already a fan of Cucumber, here is good news… Karate is made on top of Cucumber-JVM and it also uses the same Gherkin syntax. So… yes, you can start writing your BDD style tests right away. Even though you have not used any of the above mentioned Cucumber or Gherkin, it’s really easy to learn how to write tests with BDD style karate steps.

Karate let’s you write and execute API tests without writing a single line of Java code. Karate comes with it’s own predefined step definitions, and there are a lot. All the steps you need to test your API functionality is there at your fingertips.

Why use karate?

If all the above mentioned points are not enough to convince you to get started testing your web services with karate, have a look at the following points.

 

  • Karate helps to get rid of the boilerplate code which is involved in performing the requests and asserting the response contents.
  • Easily written and understood even by non-programmers.
  • Can construct the most complex request-response operations with less effort.
  • Karate sits on top of Cucumber-JVM, you can run tests and generate reports like any standard Java project.
  • Many popular text editors supports Gherkin syntax.
  • Karate can run tests in parallel, and dramatically cut down execution time. 
  • Contains a mock servlet and allows you to create mock APIs. 
  • Let’s you use scenarios as Gatling performance tests.
  • Supports Schema validations.

Getting Started with Karate

Prerequisites:

  • Java 8
  • Either Maven or Gradle
  • Either Eclipse or InteliJ IDEs

If your project is a maven project you need to add the following dependencies

If your project is a Gradle project, you need to add the following dependencies to your build.gradle

Or you can straight away create whole skeleton project with Karate Maven archetype with one command.

The above code snippet will create the project skeleton and when you import the project to your IDE it will look like this.

Now you can start writing tests. The created project will have a sample test feature. But I am going to create a separate package called “features” and write my tests inside that package.
In order to execute the test features I am about to write, karate needs a .java file created in the same package as the .feature files. The .java file will look like below.

Karate-config.js

If you look at the previously created project structure again you will see a karate-config.js file. This file let’s you add all your global variable and javaScript functions that you are going to use in your features.When you execute tests, karate is going to execute this config.js file first. This comes in handy when you have different variables when switching between environments.

Read more about environment configurations in here.

KARATE sample scenario

I bet anyone can understand the above test scenario just by reading the steps. I have only used very small amount of steps from karate in above demo. You can refer to the Karate Repository to find many more tricks you can do with karate.

When to use karate

As you have already seen, karate is a powerful framework built with all the API testing capabilities to make your life easier.
When you need to achieve higher test coverage for a complex API, need to do lots of response validations, Karate will come in very handy.
As karate can be execute with maven it can be run in your CI environment.
Karate is mostly recommended for unit testing the APIs. As the main points of having karate is to have predefined step definitions to test your API and get rid of the boiler plate code, adding more java classes and other dependencies will mess with the main goal of using karate. If you are going to stick in to testing the APIs only without having any other dependency steps, you probably will be able to do the testing without needing add any java code.
If you are following TDD or BDD it’s important that you have the tests ready as soon as possible before the development is done, with Karate you can easily achieve this as it will consume the minimum time to design the tests.

So when not to use Karate 

Even though Karate is built on cucumber-jvm, it will not allow you to add any custom step definitions as in cucumber. But you can add your steps as javaScript functions or using Java interop.

When your Rest services have many dependencies like JMS, Databases, It may not be the best idea to use Karate for testing the integrated behavior. If you try to add many dependency steps with karate, the scenarios will get unreadable and more complex.

But it does mean that you can not use JMS or Databases in karate. Look at the following code snippet of calling JDBC.

Below is a comparison of Karate Vs Cucumber done by intuit guys.

If you have experience with Rest Assured or trying to decide whether to use Rest Assured or Karate for automating API tests, here is a comparison of Karate vs Rest Assured done by intuit guys.

One more important thing to remember is that using karate will not mean you are following BDD. Karate is built on cucumber to take the advantages of cucumber capabilities, but it does not follow BDD practices. In any case, it does not matter whether it breaks BDD or not, as long as it makes your life better than today and Not worse.

Niron

Niron Rasanjana

Quality Assurance Engineer | Mitra Innovation