Q3 of 40 · Karate
Walk through the structure of a basic Karate .feature file.
KarateJuniorkaratefeature-filefundamentalsbddapi-testing
Short answer
Short answer: A .feature file starts with Feature: followed by an optional description, then one or more Scenario: blocks. Each scenario contains Given/When/Then steps — or starred * steps. A Background: block above the scenarios runs before each one. Karate steps differ from Cucumber: * works anywhere and executes immediately.
Detail
A minimal Karate feature file:
Feature: User API
Background:
* url 'https://api.example.com'
* def authHeader = { Authorization: 'Bearer test-token' }
Scenario: Get existing user
Given path '/users/1'
And headers authHeader
When method GET
Then status 200
And match response.name == 'Alice'
Scenario: Create new user
Given path '/users'
And headers authHeader
And request { name: 'Bob', email: 'bob@example.com' }
When method POST
Then status 201
And match response.id == '#uuid'
Key structural elements:
Feature:— label for the file (appears in reports)Background:— runs before every Scenario in the file (like@BeforeEach)Scenario:— one test case; steps execute top-to-bottom* url/* def— starred steps work outside Given/When/Then sequencingmethod GET/method POST— fires the HTTP request; always inWhenstatus 200— asserts the HTTP status; always inThen
The JUnit 5 runner discovers .feature files under src/test/resources — no Java test methods needed per scenario.
// EXAMPLE
product-api.feature
Feature: Product API
Background:
* url karate.properties['api.base.url']
* def adminToken = karate.properties['admin.token']
* header Authorization = 'Bearer ' + adminToken
Scenario: Get product by ID returns correct name
Given path '/products/101'
When method GET
Then status 200
And match response == { id: 101, name: '#string', price: '#number', active: true }
Scenario: Create product returns 201 with UUID id
Given path '/products'
And request { name: 'Widget', price: 9.99, category: 'tools' }
When method POST
Then status 201
And match response.id == '#uuid'
And match response.name == 'Widget'// WHAT INTERVIEWERS LOOK FOR
Correct Feature/Background/Scenario structure, knowing that Background runs before every scenario (like @BeforeEach), and understanding that method GET/POST fires the request in When. Distinguishing Karate's * steps from plain Cucumber is a differentiating signal.
// COMMON PITFALL
Writing the http method call (method GET) in the Given block. In Karate, Given configures the request and When method fires it — mirroring REST Assured's given/when structure.