skip to content

Karate

A Gherkin-flavoured DSL for API tests with no step definitions to write: payload matching, reusable feature calls, and a mock server in the same tool. Interviewers ask when that trade pays.

on this pageshow

explore

questions

90 · 5 sections

A Karate scenario reads `Given url 'https://api.example.com'`, `And path 'cats'`, `And request { name: 'Billie' }`, `When method post`, `Then status 201`. Which of those steps actually puts a request on the wire, and what does the `status` step do?

level: juniorimportance: must knowfreq 84%
basics
~20 s

The method step sends it. The url, path and request steps only accumulate state on Karate's request builder; method is what fires the call. The status step then asserts the response code and fails if it is anything else.

open as a page

In a Karate feature file, what does a step that begins with a bare `*` — such as `* def id = 5` — mean, and what changes if you write `Given`, `When` or `Then` in front of it instead?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Nothing changes. Karate accepts six step prefixes - the bare asterisk plus Given, When, Then, And and But - and treats all six identically. The prefix never reaches the code that decides what a step does; it is read back only afterwards, by the report and the rest of the output.

open as a page

In a Karate `Scenario Outline` step, what is the difference between writing `<name>` and `'#(name)'`, and which one preserves the column's type?

level: middleimportance: must knowfreq 52%
basics
~20 s

In Karate, <name> is a text substitution: the raw cell text is spliced into the step before it runs. '#(name)' is an embedded expression evaluated at run time against the bound variable, so the value keeps its type.

open as a page

In a Karate feature file, what turns an `Examples:` table into a dynamic one — so its rows come from `read('cats.json')` or `karate.setup()` instead of being typed out — and what must that cell evaluate to?

level: middleimportance: must knowfreq 60%
basics
~20 s

Karate treats an Examples table as dynamic only when it is a single cell: one column and one row, the header, with nothing under it. That cell is evaluated as JavaScript and must return a list of objects or a generator function.

open as a page

In a Karate `Examples:` table, what does appending `!` to a column header do, and what type does that column's value have without it?

level: middleimportance: must knowfreq 54%
basics
~20 s

A trailing ! on a Karate Examples column header makes each cell evaluate as a JavaScript value — number, boolean, or inline JSON. Without it the cell stays a plain string, and a blank cell becomes null.

open as a page

In a Karate feature file, what does `match response contains { id: 1 }` assert, and what does `match response.ids contains 23` assert when `ids` is a JSON array?

level: juniorimportance: must knowfreq 76%
basics
~20 s

Karate's contains asserts a subset. Against a JSON object, every expected key must exist with an equal value while extra actual keys are ignored. Against a JSON array, every expected item must appear somewhere, in any order.

open as a page

In a Karate feature file, what does the step `* match order == { id: 1, name: 'Billie' }` assert, and what happens if `order` also carries a `role` key?

level: juniorimportance: must knowfreq 84%
basics
~20 s

Karate's match == compares the entire payload in one recursive step: every key and value must correspond exactly. An extra role key fails the step, because == is a strict whole-payload comparison, not a subset check.

open as a page

In a Karate feature file, what forms can the left-hand side of a `match` step take, and what does a bare `$` refer to there?

level: juniorimportance: must knowfreq 82%
basics
~20 s

The left side of a Karate match step is a variable name, a JsonPath or XPath rooted at a variable such as response.name, a function or method call, or parenthesised JavaScript. A bare $ stands for the response, and a left side that starts with / is a bare XPath rooted there too.

open as a page

In a Karate feature file, the response carries a server-generated `id` and a `createdAt` timestamp that differ on every run. How do you still assert the entire payload in a single `match` step?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Put marker strings where the unpredictable values would go: match response == { id: '#uuid', createdAt: '#string', name: 'Billie' }. A marker asserts the value's type or shape instead of its content, so one step still checks every key.

open as a page

In a Karate feature file, what does the expected value `'#? _ > 0'` assert inside a `match`, and what is `_` bound to while that expression runs?

level: juniorimportance: must knowfreq 58%
basics
~20 s

#? runs the JavaScript that follows it and passes only when the result is true. Karate binds _ to the actual value at that position, so '#? _ > 0' asserts that the value there is greater than zero.

open as a page

In a Karate feature file, a `Background` runs before every scenario — so how does `* def token = callonce read('auth-token.feature')` avoid re-running that feature for each one?

level: juniorimportance: must knowfreq 70%
basics
~20 s

callonce runs the called feature only the first time and caches what it returned, so every later scenario in that feature file reuses it. Plain call re-executes the login for every scenario and every Examples row.

open as a page

In a Karate feature suite, what must the `karate-config.js` file contain, and what does Karate do with the value it produces?

level: juniorimportance: must knowfreq 78%
basics
~20 s

karate-config.js holds a JavaScript function that returns a map. Karate evaluates it before every top-level scenario and applies each key of that map as a scenario variable, so no step-definition or glue class is involved.

open as a page

In a Karate feature file, what is the difference between the step `* configure headers = { 'X-Trace': 'abc' }` and the step `* header X-Trace = 'abc'`?

level: juniorimportance: must knowfreq 70%
basics
~20 s

configure headers sets a default header map that Karate re-applies to every later request in scope. The header step sets one header on the request being built, and Karate clears it as soon as that call completes.

open as a page

In a Karate feature file, what is the difference between `* call read('login.feature')` and `* def auth = call read('login.feature')`?

level: juniorimportance: must knowfreq 70%
basics
~20 s

The def assignment decides scope. A bare call read('login.feature') merges every variable the called feature defined into the caller; def auth = call read('login.feature') isolates the call and hands those variables back as a map instead.

open as a page

In Karate, how do `callonce` and `karate.callSingle()` differ in how wide their cache is and in what each one uses as the cache key?

level: middleimportance: must knowfreq 62%
basics
~20 s

callonce caches per feature and keys on the call expression exactly as written. karate.callSingle() caches per suite, so once for the whole run across every feature, and keys on the path string you pass it.

open as a page

In a Karate mock feature - the kind served by `karate.start()` or a `MockServer` - when do the `Background` steps run?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Once, when the mock server starts. Karate executes a mock feature's Background while the handler is being built, before the first request arrives; every request afterwards runs only the steps of the one Scenario whose name matched.

open as a page

In a Karate mock feature file, what is the text after `Scenario:` used for, and what does the mock return when no `Scenario` matches the incoming request?

level: juniorimportance: must knowfreq 66%
basics
~20 s

In a Karate mock feature the Scenario name is a JavaScript expression evaluated per request, not a label. Scenarios are tried in file order; the first one returning true answers, and if none does the mock replies 404.

open as a page

In a Karate mock feature file, how does a matched Scenario produce the HTTP reply — what do you write instead of returning a response object?

level: juniorimportance: must knowfreq 72%
basics
~10 s

A mock scenario returns nothing. It assigns ordinary variables — response, responseStatus, responseHeaders, responseDelay — and after the last step Karate reads them and assembles the reply, so declaration order does not matter.

open as a page

A Karate mock feature has to remember data between HTTP requests. Where does that state live, and what puts it there?

level: middleimportance: must knowfreq 60%
basics
~20 s

In a map of global variables owned by the mock handler. The Background seeds it once at start-up, and after every matched Scenario the handler copies that request's variables back, so a def inside a Scenario survives too.

open as a page

In a Karate mock feature, what does `Scenario: pathMatches('/cats/{id}')` match, and where does the `id` value become available?

level: middleimportance: must knowfreq 58%
basics
~20 s

It matches a request path of exactly two segments whose first is literally cats, capturing the second under the name id. On a successful call the helper fills the pathParams map, so the scenario body reads pathParams.id as a string.

open as a page

Karate and a Java API-testing library such as REST Assured both drive HTTP calls and assert on responses. What is written differently in a Karate project, and what does that remove?

level: juniorimportance: must knowfreq 72%
basics
~20 s

In Karate the test is a .feature file whose steps are the framework's own keywords, so there is no step-definition layer to write: the HTTP call, the request payload and the assertion all live in that one file.

open as a page

In Karate, what does the Java line `Runner.path("classpath:animals").tags("@smoke").parallel(5)` actually do, and why does this class need no step-definition or glue package?

level: juniorimportance: must knowfreq 76%
basics
~20 s

Runner.path() opens Karate's builder, each chained call sets one option, and parallel(n) is the terminal call that discovers the features, runs them on n threads and returns a result object. No glue package is involved.

open as a page

A team moves its API suite from a Java library like REST Assured onto Karate feature files. Which compile-time guarantees does that give up, and what does Karate offer in their place?

level: middleimportance: must knowfreq 58%
basics
~10 s

Every compile-time guarantee goes: a Karate step is text and its expressions are JavaScript, so variable, field and method names are unchecked until the scenario runs. Karate replaces them with runtime failure and Java.type.

open as a page

In a Karate Java runner, what does `Runner.path("classpath:api").tags("@smoke,@sanity", "~@wip")` select, and how do the comma, the tilde and the two separate arguments each combine?

level: middleimportance: must knowfreq 68%
basics
~10 s

That chain runs scenarios tagged @smoke or @sanity but not @wip. Separate arguments are ANDed, a comma inside one argument means OR, and a leading tilde negates that whole argument.

open as a page

In Karate's JUnit integration, what is the `@Karate.Test` annotation, where may it be placed, and what must the annotated method return?

level: juniorimportance: should knowfreq 42%
basics
~10 s

Karate's @Karate.Test is a method-level annotation meta-annotated with JUnit's @TestFactory. The method must return a Karate instance, an Iterable of dynamic nodes, so each feature and scenario becomes its own test.

open as a page