Q8 of 40 · Karate
What's the difference between `match ==` and `match contains` in Karate?
KarateJuniorkaratematchcontainsassertionsfundamentals
Short answer
Short answer: match == requires exact equality — every field in the response must match and no extra fields are allowed. match contains checks that the listed fields are present with the correct values but ignores additional fields. Use == for strict contract tests; use contains when you only care about a subset of the response.
Detail
match == — full structural equality:
* match response == { id: 1, name: 'Alice', active: true }
# FAILS if response also has email: 'a@b.com' (extra field)
# FAILS if name is 'alice' (case-sensitive)
match contains — subset check:
* match response contains { name: 'Alice' }
# PASSES even if response has id, email, timestamps, etc.
# Only asserts that name == 'Alice' is present
Array variants:
match response.items contains 'apple'— array contains the itemmatch response.items contains only ['apple', 'banana']— array contains exactly these (order-insensitive)match response.items !contains 'cherry'— array does not contain the item
When to use each:
==for strict contract tests (schema + values), especially in contract testing where adding a field is a breaking changecontainsin step-chained scenarios where the earlier step added fields you don't care about for this assertioncontains onlyfor testing that a list has exactly these elements regardless of order
// EXAMPLE
match-comparison.feature
Feature: Demonstrating match == vs match contains
Scenario: Strict equality vs partial match
* def response = { id: 1, name: 'Alice', email: 'alice@example.com', active: true }
# PASSES — exact match, all fields listed
* match response == { id: 1, name: 'Alice', email: 'alice@example.com', active: true }
# FAILS — response has email field not listed here
# * match response == { id: 1, name: 'Alice' }
# PASSES — contains only checks listed fields
* match response contains { id: 1, name: 'Alice' }
# Array examples
* def fruits = ['apple', 'banana', 'cherry']
* match fruits contains 'apple' # passes
* match fruits contains only ['banana', 'apple', 'cherry'] # passes (order-insensitive)
* match fruits !contains 'grape' # passes (negation)// WHAT INTERVIEWERS LOOK FOR
Clear articulation of exact vs subset semantics, the array variants (contains, contains only, !contains), and situational judgment on when to use each. Using == for contract tests and contains for partial assertions in chained scenarios shows practical experience.
// COMMON PITFALL
Using match contains everywhere to avoid failing on extra fields — this makes tests too permissive and misses structural regressions like unexpected field removal or type changes on unlisted fields.