Introduction

Assertions are important part of automated testing, helping you verify that the output of your application is correct. The Karate framework simplifies the process of writing assertions for API testing. In this post, we will explore assertions in Karate framework using clear examples, simple language, and practical tables. By the end of this article, you will be confident in using Karate for effective API testing.
What is an Assertion in Karate?
Assertion is use to check whether a condition or expected result is true. In the Karate framework, assertions are used to compare actual API responses with expected values. If the comparison fails, the test is marked as failed. Assertions help us to ensure that our APIs response is expected.
Types of Assertions in Karate Framework
Karate allows you to do different type of assertions. Here are some common types:
Assertion Type | Description |
---|---|
Status Code Assertion | Verifies the HTTP response status code. |
JSON Path Assertion | Checks specific values within a JSON response. |
Schema Assertion | Verifies the structure of the response. |
Contains Assertion | Ensures that a value or element exists in the response. |
Equals Assertion | Confirms that values are equal. |
1. Status Code Assertion
One of the simplest assertions is verifying the status code of an API response. You can easily check if the response code is 200
, indicating a successful request.
karateCopy codeFeature: Status Code Assertion in Karate
Scenario: Verify 200 Status Code
Given url 'https://jsonplaceholder.typicode.com/posts/1'
When method get
Then status 200
In this example:
- The API request is sent to a URL.
- The
Then status 200
assertion checks if the response code is200
.
2. JSON Path Assertion
You can use JSON Path to extract specific fields from the response and assert their values. This is particularly useful for validating specific data points in the response body.
karateCopy codeFeature: JSON Path Assertion
Scenario: Verify JSON Response
Given url 'https://jsonplaceholder.typicode.com/posts/1'
When method get
Then status 200
And match response.id == 1
In this scenario:
- The
And match response.id == 1
checks if theid
field in the JSON response is1
.
3. Schema Assertion
Karate allows you to verify the structure or schema of the JSON response to ensure it matches the expected format.
karateCopy codeFeature: Schema Assertion
Scenario: Validate JSON Schema
Given url 'https://jsonplaceholder.typicode.com/posts/1'
When method get
Then status 200
And match response == { id: 1, title: '#string', body: '#string', userId: '#number' }
In this example:
- The
And match response == { id: 1, ... }
assertion checks if the response contains the expected structure. It also checks the types, ensuring thattitle
andbody
are strings, whileuserId
is a number.
4. Contains Assertion
The contains
assertion is useful when you need to verify if a particular value or field exists in the response.
karateCopy codeFeature: Contains Assertion
Scenario: Verify Response Contains Value
Given url 'https://jsonplaceholder.typicode.com/posts'
When method get
Then status 200
And match response contains { id: 1, title: 'some title' }
Here:
- The
And match response contains { id: 1, title: 'some title' }
checks if the response contains an item with theid
of1
and thetitle
ofsome title
.
5. Equals Assertion
The equals assertion helps verify whether two values are identical. This can be applied to both status codes and specific fields within a JSON response.
karateCopy codeFeature: Equals Assertion
Scenario: Verify Field Equality
Given url 'https://jsonplaceholder.typicode.com/posts/1'
When method get
Then status 200
And match response.userId == 1
In this example:
- The
And match response.userId == 1
assertion verifies that theuserId
field in the JSON response is equal to1
.
Using Tables for Data-Driven Assertions
Karate makes it easy to work with data-driven tests using tables. You can use tables to define multiple test cases in a single scenario.
Example: Data-Driven Assertion Using Tables
karateCopy codeFeature: Data-Driven Test Using Tables
Scenario Outline: Verify Status Code for Different Posts
Given url 'https://jsonplaceholder.typicode.com/posts/<id>'
When method get
Then status 200
And match response.id == <id>
Examples:
| id |
| 1 |
| 2 |
| 3 |
In this example:
- The table under
Examples
allows you to run the same test for different post IDs (1
,2
,3
). - The
Scenario Outline
repeats the test for each row in the table, asserting that theid
in the response matches the value in the table.
Best Practices for Assertions in Karate Framework

To make the most out of assertions in the Karate framework, follow these best practices:
- Keep assertions clear and simple: Avoid overly complex assertions to ensure that your tests remain easy to understand.
- Use JSON Path efficiently: Focus on extracting specific fields you need to validate.
- Test for multiple scenarios: Use Karate’s data-driven approach to cover various cases.
- Verify not only success: Test for both successful and error responses, covering all relevant status codes.
Conclusion for Assertion in karate Framework
In this blog post, we have explored assertions in Karate framework by using practical examples and simple explanations. Karate provides a variety of assertion types from verifying status codes to checking the structure of JSON responses that make API testing easier and more effective.
Here’s a quick summary of the assertions covered:
- Status Code Assertion: Checks if the status code matches the expected result.
- JSON Path Assertion: Validates specific fields in a JSON response.
- Schema Assertion: Ensures that the response matches the expected structure.
- Contains Assertion: Verifies if a particular value exists in the response.
- Equals Assertion: Checks if two values are equal.