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
- Leverages easy-to-understand and super-readable Gherkins language
- Based on Cucumber standards
- Supports logging, debugging and parallel testing
- Built-in reporting
- CI/CD integration
- It can call other feature files, JS functions, and even Java methods.
- Supports YAML, XML, CSV, and many more.
- 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.
6 thoughts on “Karate Series | Session 1 | Karate Basics”