Karate API Testing - Getting Started
Karate is a powerful open-source tool for API testing that combines API testing, mocking, and performance testing in a single framework.
π― Why Karate?β
- No Java/Programming Knowledge Required - Uses Gherkin syntax
- Built-in JSON/XML Support - Native data handling
- Powerful Assertions - Rich assertion capabilities
- Test Reports - Cucumber-style HTML reports
- Performance Testing - Built-in Gatling integration
π¦ Installationβ
Prerequisitesβ
- Java 8+ installed
- Maven or Gradle
Maven Setupβ
Add to your pom.xml:
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit5</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>
π Your First Karate Testβ
Create a feature file users.feature:
Feature: User API Testing
Background:
* url 'https://jsonplaceholder.typicode.com'
* header Accept = 'application/json'
Scenario: Get all users
Given path 'users'
When method get
Then status 200
And match response[0].name == '#string'
And match response[0].email == '#string'
Scenario: Create a new user
Given path 'users'
And request { name: 'John Doe', email: 'john@example.com' }
When method post
Then status 201
And match response.name == 'John Doe'
π§ Key Featuresβ
JSON Path & Assertionsβ
# Simple assertions
Then match response.status == 'success'
Then match response.users[0].name == 'John'
# Schema validation
Then match response ==
"""
{
"id": "#number",
"name": "#string",
"active": "#boolean"
}
"""
# Array validation
Then match response.users == '#[3]' # Array with 3 elements
Data-Driven Testingβ
Scenario Outline: Test multiple users
Given path 'users/<id>'
When method get
Then status 200
And match response.name == '<name>'
Examples:
| id | name |
| 1 | John |
| 2 | Jane |
π Test Executionβ
Run with Mavenβ
mvn test
Run specific featuresβ
mvn test -Dkarate.options="--tags @smoke"
π Reportsβ
Karate generates beautiful HTML reports at:
target/karate-reports/karate-summary.html
π Next Stepsβ
- Advanced Karate Features (coming soon)
- API Security Testing (coming soon)
- Performance Testing with Gatling
Master API testing with Karate! π₯