Powerful HTTP end-to-end tests with Karate
Problem: Time-consuming E2E testing of HTTP endpoints
E2E testing of HTTP endpoints can be time-consuming, especially when dealing with elaborate JSON or XML requests. You could of course use frameworks such as Postman, Rest-Assured or Spring MockMvc, but in turn they suffer from issues like tracking of changes, setup of the environment, maintenance of test data, or ease of test execution and the corresponding reporting.
Solution: The Karate Framework With Direct Support of JSON and XML
Karate is a framework to smoothly implement E2E tests with direct support of JSON and XML for requests and validation of responses as well as interoperability with Java and JavaScript. Test cases are formulated semi-naturally in Gherkin – good POs provide this as acceptance criteria in their stories! Test results are summarized in reports, which contain the HTTP exchange and the assertions. With a little extra effort, you can reuse tests and answers from previous tests. In addition, Karate comes with a mock server and even performs load tests.
Example for End-to-End Tests with Karate
The following example checks an XML response where the order is irrelevant (you might compare the actual response from mocky.io with the expectation in the test).
Feature: jambit API
Scenario: Checking mission and motto collectively
Given url "http://www.mocky.io/v2/5bd9ab282f00005c0006d262"
And header Accept = "application/xml"
When method get
Then status 200
And match response contains
"""
<jambit>
<mission>100% Begeisterung</mission>
<motto>Where innovation works</motto>
</jambit>
"""
The following test suite defines the URL for subsequent test cases and then the parameterized test randomly checks three expectations.
Feature: Users API
Background:
* url "https://jsonplaceholder.typicode.com/"
Scenario Outline: Given user ID, validate company name
Given path "users", <id>
When method get
Then status 200
And match response.company.name == "<companyName>"
Examples:
| id | companyName |
| 1 | Romaguera-Crona |
| 3 | Romaguera-Jacobson |
| 7 | Johns Group |
---
Author
Alexandros Panagiotidis / Senior Software Architect / Office Stuttgart
Download Toilet Paper #103: Powerful HTTP end-to-end tests with Karate (pdf)