Create your First Rest API Test using Karate.

Karate allows you to define automated tests in gherkin (Given, When, Then) which are executed directly as either Web Browser or API tests.

  1. Under src/test/java, create a new file called Jenkins.feature
    2. Enter the following:

     

    Click Run.

Your test should pass.

Example 2:

 

Karate Rest Test Tool – No Code Required!

What’s cool about Karate is that unlike most BDD frameworks (Cucumber, JBehave and SpecFlow), you don’t need to write step definitions. The reason for that is that Karate has already created all the step definitions you need in order to get started with JSON, HTTP or XML.

You don’t even need to write Java code! You can create your API test in a BDD syntax without the overhead of writing any code to implement your scenarios.

Rest API Test Tool Using CucumberJVM

Since Karate’s REST test tool is built on top of Cucumber-JVM, you can run your tests, view reports and leverage any other Cucumber functionality just as with any standard Java project.

In addition, the “Getting Started” guide for Karate recommends using jUnit — NOT TestNG — since things like dynamic tables, data-driven testing and tag groups are already built in.

 

1-API Request Methods

As you know, there are different API request methods such as “Get”, “Put”, “Update” etc. Calling an endpoint using with API request method is very simple by Karate as below. Just type request method which you need after “method” keyword.

When method GET
When method POST
When method DELETE
When method PATCH

2-Assertion and Validation

Assertions and Validations are the key points of the test processes to check responses or expected results. It’s not possible to make sure that the application without assertions or validations of test cases is working correctly.

At this point, Karate emerges a variety of solutions to verify test cases.

  • Status Code Check

Then status 200

  • Response Value Check

And match product.data[0].name == ‘We are KLOIANs’

  • Response Contains Key-Value Check

And match response contains {price: 2.5}

3-Reading Files

Reading files is a common need in test automation project to re-use or manipulate payload data, scripts or functions. Especially I’ve faced a need to read and modify DTOs many times in the projects. When focused on reading files, Karate supports to read .json, .xml, .yaml, .js, .csv or .txt data file type.

* def json = read(‘some.json’)
* set json.item[] = ‘string’
And request read(‘product-update-data.json’)

4-Calling Feature File or Scenario

Developing same test cases and codes over and over again is a problem for development . To avoid writing repetitive scenarios or steps, use “call” method. “call” method can be used for whole feature file or a specific scenario by its tag.

Given call read(‘call.feature’)
Given call read(‘classpath:call.feature@tag’)