×
☰ See All Chapters

Karate Tutorial - REST web service testing

This tutorial is an introduction to REST web service Testing using Karate Framework. Nowadays It has become common term calling REST web service as REST API, hence we can say Karate is used to test REST API. Karate framework is built on top of the popular Cucumber framework and using Gherkin Language.  If you don’t possess knowledge of cucumber and gherkin, we suggest you understand and learn gherkin language before starting Karate.  Please go through our Cucumber tutorial and Gherkin Tutorial before you start reading Karate framework.

Karate framework can be used by anyone and test cases can be read and written very easily. Karate framework uses is a gherkin which is ubiquitous language and understood by everyone involved in the project. Customers can specify expectations for the web services response easy and fast, offering a custom domain-specific language. By reading the test cases any one can visualize the system before it has been built. Anyone on the team can read a test case and reply their understanding of what the system should do, and it may well spark their imagination into thinking of other scenarios that you’ll need to consider too.

By writing test cases customer expresses how the system should behave after its complete development. This process of test-driven development is called as behaviour driven development. Behaviour Driven Development (BDD) builds upon Test Driven Development (TDD) by changing the test cases to expected behaviours.

Below is a simple syntax of karate acceptance test:

Feature: Testing sample service

 

  Scenario: Testing sample service for success response

    Given url 'https://localhost:8080/test/hello'

    When method GET

    Then status 200

Features of Karate

  1. You don't need to write extra "glue" code or Java step definitions for gherkin steps. If you are using Java with maven, then you just need to use Karate dependencies in your pom.xml. 

  2. Test Script files (feature files) are plain text files doesn’t require any compilation steps.  

  3. Can cover multiple scenarios using single script (feature) file. 

  4. Scripts are re-usable that may be called from other scripts and feature files. 

  5. Native’ JSON and XML support including JsonPath and XPath expressions. 

  6. Embedded JavaScript engine that allows you to build a library of re-usable functions that suit your specific environment or organization. 

  7. Built-in support for switching configuration across different environments (e.g. dev, QA, pre-prod) 

  8. Support for data-driven tests and being able to tag or group tests is built-in, no need to rely on an external framework 

  9. Native support for reading YAML and even CSV files - and you can use them for data-driven tests. 

  10. Multi-threaded parallel execution, which is a huge time-saver, especially for integration and end-to-end tests. 

Many other features are listed on the GitHub project. And below is the complete Karate Index which you can refer for more details.

Start

Maven | Gradle | Quickstart | Standalone Executable | Naming Conventions | Script Structure

Run

JUnit 5 | Command Line | IDE Support | Tags / Grouping | Parallel Execution | Java API | jbang

Report

Configuration | Environment Switching | Reports | JUnit HTML Report | Dry Run | Report VerbosityLogging | Log Masking

Types

JSON | XML | JavaScript Functions | Reading Files | Type / String Conversion | Floats and Integers | Embedded Expressions | JsonPath | XPath | Karate Expressions

Variables

def | text | table | yaml | csv | string | json | xml | xmlstring | bytes | copy

Actions

assert | print | replace | get | set | remove | configure | call | callonce | eval | listen | doc | read() | karate JS API

HTTP

url | path | request | method | status | soap action | retry until

Request

param | header | cookie | form field | multipart file | multipart field | multipart entity | params | headers | cookies | form fields | multipart files | multipart fields

Response

response | responseBytes | responseStatus | responseHeaders | responseCookies | responseTime | responseType | requestTimeStamp

Assert

match == | match != | match contains | match contains only | match contains any | match contains deep | match !contains | match each | match header | Fuzzy Matching | Schema Validation | contains short-cuts

Re-Use

Calling Other *.feature Files | Data Driven Features | Calling JavaScript Functions | Calling Java Code | Commonly Needed Utilities | Data Driven Scenarios

Advanced

Polling | Conditional Logic | Before / After Hooks | JSON Transforms | Loops | HTTP Basic Auth | Header Manipulation | GraphQL | Websockets / Async | call vs read()

More

Mock Servlet | Test Doubles | Performance Testing | UI Testing | Desktop Automation | VS Code / Debug | Karate vs REST-assured | Karate vs Cucumber | Examples and Demos

 

 


All Chapters
Author