Karate Series | Session 1 | Karate Basics

What we explore in this series

Karate Basics
GET API
POST API
PUT API
DELETE API
Karate Config
Karate Reports

WHAT ?

According to karatelabs (https://github.com/karatelabs/karate), Karate is the only open-source tool to combine API test-automation, mocks, performance-testing, and even UI automation into a single, unified framework.

It uses Behaviour Driven Development(BDD) syntax for writing scenarios, which is easy for even non-programmers. It contains built-in assertions and reporting mechanism, and runs tests in a parallel fashion for speedy execution. The compile-free code written in a simple and readable syntax of Karate helps our life easy to develop and maintain projects.

WHEN ?

It was created by Peter Thomas in 2017, and aimed to make testing functionalities readily available for everyone. It is the extension of Cucumber, written in Java, and uses Gherkins files.

FEATURES

  1. Leverages easy-to-understand and super-readable Gherkins language
  2. Based on Cucumber standards
  3. Supports logging, debugging and parallel testing
  4. Built-in reporting
  5. CI/CD integration
  6. It can call other feature files, JS functions, and even Java methods.
  7. Supports YAML, XML, CSV, and many more.
  8. Async support

HOW ?

The following dependencies will be used with Maven in pom.xml

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

If we wanted to enable Cucumber reporting, the following dependency is also to be added.

<dependency>
   <groupId>net.masterthought</groupId>
   <artifactId>cucumber-reporting</artifactId>
   <version>5.3.0</version>
</dependency>

Karate test scripts are written in .feature files, which is inherited from Cucumber. Ideally feature files are kept in src/test/java and java files are in src/main/java. While using Karate in a Java / Springboot project, the creators suggested keeping *.java and *.feature files side by side as it is much easier to look out for both files when they are kept together. If we keep them together, we can exclude *.java files in the build script like this in Maven.

<build>
   <testResources>
      <testResource>
        <directory>src/test/java</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </testResource>
   </testResources>
   <plugins>
     ...
   </plugins>
</build>

The Karate framework can have Runner class and in most cases the class name would be TestRunner.java

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

@RunWith(Karate.class)
@KarateOptions(features = "classpath:features", tags = "~@ignore")
public class TestRunner {
  ...
}

@Runwith says Karate.class is the runner for this test. The features inside @KarateOptions tells the framework where to scan for feature files (I’ve kept my feature files inside the features folder in the classpath). The tags tells the framework to run/skip features/scenarios. Here we put ~@ignore and due to which those features/scenarios on which this annotation is marked will be excluded.

Published by

Deepesh Darshan K R

I am Me. In all the world, there is no one else exactly like me. Everything that comes out of me is authentically mine, because I alone chose it. I own everything about me: my body, my feelings, my mouth, my voice, all my actions, whether they be to others or myself. I own my fantasies, my dreams, my hopes, my fears. I own my triumphs and successes, all my failures and mistakes. Because I own all of me, I can become intimately acquainted with me. By so doing, I can love me and be friendly with all my parts. I know there are aspects about myself that puzzle me, and other aspects that I do not know -- but as long as I am friendly and loving to myself, I can courageously and hopefully look for solutions to the puzzles and ways to find out more about me. However I look and sound, whatever I say and do, and whatever I think and feel at a given moment in time is authentically me. If later some parts of how I looked, sounded, thought, and felt turn out to be unfitting, I can discard that which is unfitting, keep the rest, and invent something new for that which I discarded. I can see, hear, feel, think, say, and do. I have the tools to survive, to be close to others, to be productive, and to make sense and order out of the world of people and things outside of me. I own me, and therefore, I can engineer me. I am me, and . . . . . . . . . . I am Okay. Haha.. Let it go.. I am a software engineer working in a MNC here at Kochi itself, specialized on J2EE platform, dribbling with frameworks like struts, hibernate etc.

6 thoughts on “Karate Series | Session 1 | Karate Basics”

Leave a comment