Search:

Mastering Karate in Five Steps - Step 1: Introduction to Karate, Project Setup, Hello world

Karate Framework for API Test Automation; Welcome to Karate World.

Step-1: Introduction to Karate, Project Setup, Hello world

One of our customers asked us to suggest an API testing tool for creating their API testing suite from scratch. They had some requirements for that tool like easy to learn, less development and maintenance cost, running API tests as performance testing, integrated with CI/CD tools, etc. While we, Kloians, are researching new tools for automated API testing for that customer, we realized that there is one great tool, the Karate Framework. Karate has lots of great features and is very straightforward. We have started coding their framework from scratch. We have learned many significant components of the Karate Framework, and we just wanted to let you know these features because we love open source. So we have started to write this blogpost series which includes five parts, and introduce the Karate Framework to you. I hope you will enjoy and learn.

Karate Framework

Karate Framework is an open-source framework that combines automated API tests, performance testing, mocks, and even UI testing into a single framework. Peter Thomas from Intuit developed the framework, and it has a large community. 

Karate uses Gherkin syntax and BDD approach for coding test scripts, so you don't need to know any programming language such as Java or Python. If you want to know more about BDD, you can look at our blogpost on BDD

Karate has predefined step definitions, and you are not responsible for defining the required steps. You only need to have a basic understanding of API testing and Gherkin knowledge to develop API tests with Karate Framework. 

Sounds good, right? But there is more. Also, you can add Gatling into your framework and use your scripts for performance testing. You are responsible only for defining the load model as a Scala class, and the same features will become your performance suites. 

In addition to the features above, you can use Karate for mocking your services and perform contract testing. I believe this feature is also handy and makes contract testing very easy. 

Now let's start to learn how we can use Karate Framework in API testing.

Requirements: 

Let’s start by setting up our environment. Karate is supported by Java 8-12, and you can use any of these Java versions (After the Karate 1.0 version it supports all the versions). I recommend you to use Java 8 since it is the most popular long-term supported Java version. Besides Java, you need to have either Maven, Gradle, IntelliJ, or Eclipse installed. In this article, I will use IntelliJ IDE Community Edition. I have also installed Maven already and suggest installing it to run your scripts on the command line. 

In addition to the above IDEs or build tools, you can use the Visual Studio Code with the extension of karate, and you can develop your scripts in Visual Studio Code. Visual Studio Code also provides the debug features, which is very useful. Now let's continue with the project setup.

Setup 

To create a Karate project, you can use the quick start command to have a ready-to-go Karate project if you don't have any previous Java development experience. Here is the command for the ready-to-go project setup:


mvn archetype:generate \
-DarchetypeGroupId=com.intuit.karate \
-DarchetypeArtifactId=karate-archetype \
-DarchetypeVersion=0.9.6 \
-DgroupId=com.mycompany \
-DartifactId=myproject
    

If you have some Java development experience and knowledge of project structure, you can create your maven project on IntelliJ IDEA. You should only add related dependencies in your pom.xml file and sync your project. Here are the associated dependencies for the beginning of Karate setup:


<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-junit5</artifactId>
    <version>0.9.6</version>
    <scope>test</scope>
</dependency>
    

Instead of JUnit 5, you may want to use JUnit 4. If you do so, change the junit5 to junit4 and sync your pom.xml file.

Folder structure

In the traditional maven project folder structure, you can keep your non-Java file under src/test/resources. But in Karate, it is better to keep all of them together since you will have a little .java, .js, or some other files, and when your projects become complicated, it is more convenient to see all of them together in the same place. You can exclude the files you don't want to specify as a test source easily in the Maven build section:


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

Since you are writing the test scripts, there is no need to be bound to naming conventions like com.company.some.example. Karate suggests keeping your folder one or two levels deep where your folder describes resources, entity, or API under the test. Here is one example of the package structure:


src/test/java
    |
    +-- karate-config.js
    +-- logback-test.xml
    +-- some-reusable.feature
    +-- some-classpath-function.js
    +-- some-classpath-payload.json
    |
    \-- animals
        |
        +-- AnimalsTest.java
        |
        +-- cats
        |   |
        |   +-- cats-post.feature
        |   +-- cats-get.feature
        |   +-- cat.json
        |   \-- CatsRunner.java

        |
        \-- dogs
            |
            +-- dog-crud.feature
            +-- dog.json
            +-- some-helper-function.js
            \-- DogsRunner.java
    

Hello world

Now your API testing framework is ready. The next step is using this Karate Framework, coding your first test case, and verifying that your project is up and running. When learning a new programming language, the first thing to do is print 'hello world' on the console. Let's see how you can do a  “hello world” in Karate.

First, create a feature file with right-click on any package you want > New > File and write any name of the feature file with .feature extension. It will present a new feature file. The next step is naming the feature. After the Feature: keyword, you can give any name that explains the task in the feature file.

Now, you have the feature file that you can use to write your test cases. For doing this, you should use Scenario: keywords to define your test cases. Here is the first Karate scenario, hello world, and run this scenario to verify project setup is successful and ready.


Feature: Hello world feature

  Scenario: Hello world
    * print 'hello world'
    

To run and verify the scenario above, you should click the green arrow button near the scenario, and then you will see the result on the console. 

karate


09:42:09.143 [main] INFO com.intuit.karate - [print] hello world


Now you are in the Karate world. As you see, it is pretty easy to write in Karate. It is pure Gherkin syntax, and you will adapt it in a short time. 

In this article, I have talked about the introduction to Karate, project setup, and how to run your first test case in Karate.

In the following article, I will discuss variables, simple API requests, and assertions in Karate

 

>>Step-2: Variables, Karate-config, API requests, Assertions

 

Stay on the line and keep learning...

Resources: https://github.com/intuit/karate

Click to download the Karate CheatSheet

Selcuk Temizsoy

Experienced Software Test Automation Engineer working in different industries. Software Test Consultant at kloia