Karate is a versatile and powerful framework for API testing, offering a simple, BDD-style syntax that integrates API testing, performance testing, and even UI testing. It is built on top of Cucumber and Gherkin but extends these with specific functionality for testing APIs.

Why Use Karate?

FeatureBenefit
API TestingBuilt-in support for testing REST, SOAP, and GraphQL APIs
Performance TestingDirect support for performance testing (e.g., load tests)
BDD SyntaxReadable tests with Gherkin-style syntax (Given/When/Then)
Parallel ExecutionEasy parallel test execution
Integrated AssertionsBuilt-in assertions for JSON/XML responses

1. Setup Karate in Maven

Add the dependencies to your pom.xml:

<dependencies> <!-- Karate Core --> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-core</artifactId> <version>1.2.0</version> <scope>test</scope> </dependency> <!-- Karate JUnit Runner (for running tests in JUnit) --> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit5</artifactId> <version>1.2.0</version> <scope>test</scope> </dependency> </dependencies>

2. Example Karate Feature (src/test/java/features/login.feature)

Karate uses Gherkin syntax for writing the tests.

Feature: User Login API Test Scenario: Successful login Given url 'https://reqres.in/api' And path 'login' And request { "email": "eve.holt@reqres.in", "password": "cityslicka" } When method post Then status 200 And match response.token == '#notnull'

3. Running Tests with JUnit5

Create a test runner class:

import com.intuit.karate.junit5.Karate; class LoginTest { @Karate.Test Karate testLogin() { return Karate.run("login").relativeTo(getClass()); } }

This will run the login.feature file using JUnit.

4. Karate Configuration (karate-config.js)

If you need to set some configurations or environment variables, you can do so in karate-config.js:

function fn() { var config = { apiUrl: 'https://reqres.in/api' }; return config; }

You can then use karate-config.js in your feature files like this:

Given url apiUrl

5. Run Tests

Use Maven to run the tests:

mvn test

After running the tests, you can find the report at:

target/karate-reports/karate-summary.html

Reporting

Karate automatically generates HTML and JSON reports, which contain detailed information about each test step, including assertions, responses, and failures.

API Performance Testing (Example)

You can also perform performance testing directly in Karate:

Feature: Performance Test Scenario: Load test for login API Given url 'https://reqres.in/api/login' And request { "email": "eve.holt@reqres.in", "password": "cityslicka" } When method post Then status 200 And print 'Response time is: ' + karate.responseTime And match karate.responseTime < 1000

You can set load scenarios by using Karate’s built-in support for performance testing, specifying things like virtual users, request intervals, etc.

6. Advanced Features

  • Data-Driven Tests: You can load data from CSV, Excel, or JSON files.

  • Mocking Servers: Karate has built-in support to mock API endpoints.

  • Parallel Execution: Run scenarios in parallel to speed up tests.