Karate is a test automation framework created to make your web services testing simple.
Kick off web services testing with KARATE
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
- 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.
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
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
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.