Skip to content

Steps Given Context

Jörg Flade edited this page Jun 24, 2026 · 2 revisions

Given — Context

These steps write fixed values into ScenarioContext before the test runs. Once stored, any step that accepts a string argument checks the context first — if the string matches a key, the stored value is substituted automatically.

See Concepts for a full explanation of how context substitution works.


Set a Static Value to the Context

Given that the context contains the key {string} with the value {string}

Writes a single key/value pair to the context.

Scenario: Use a static value in a URL path
  Given that the context contains the key "userId" with the value "abc-123"
  When executing an authorized GET call to "/api/v1/users/${userId}"
  Then I ensure that the status code of the response is 200

Especially useful in Scenario Outline to parameterize the URL or body:

Scenario Outline: Test multiple resource types
  Given that all URLs are relative to "/api/v1"
  And that the context contains the key "resourceType" with the value "<type>"
  When executing an authorized GET call to "/${resourceType}"
  Then I ensure that the status code of the response is 200

  Examples:
    | type     |
    | users    |
    | orders   |
    | products |

Make sure keys are unique across steps to avoid one step overwriting another's value.


Set Multiple Static Values to the Context

Given that the context contains the following 'key' and 'value' pairs
  | key1 | value1 |
  | key2 | value2 |

Writes multiple key/value pairs in one step. The data table has no header row — each row is a key/value pair.

Scenario: Build a nested URL from multiple context values
  Given that the context contains the following 'key' and 'value' pairs
    | tenantId   | acme-corp |
    | resourceId | res-42    |
  When executing an authorized GET call to "/api/v1/${tenantId}/resources/${resourceId}"
  Then I ensure that the status code of the response is 200

Scenario: Populate a request body template from context
  Given that the context contains the following 'key' and 'value' pairs
    | FIRST_NAME | Alice   |
    | LAST_NAME  | Schmidt |
  And that the body of the request is
    """
    {
      "firstName": "${FIRST_NAME}",
      "lastName":  "${LAST_NAME}"
    }
    """
  When executing an authorized POST call to "/api/v1/users" with previously given body
  Then I ensure that the status code of the response is 201

Reset the Scenario Context

Given that the stored data in the scenario context map has been reset

Clears all custom entries from the context map. Useful when a single scenario contains multiple independent request/response cycles and you want to start fresh between them.

Scenario: Two independent requests in one scenario
  Given that the context contains the key "resource" with the value "alpha"
  When executing an authorized GET call to "/api/v1/${resource}"
  Then I ensure that the status code of the response is 200

  Given that the stored data in the scenario context map has been reset
  And that the context contains the key "resource" with the value "beta"
  When executing an authorized GET call to "/api/v1/${resource}"
  Then I ensure that the status code of the response is 200

Reset the Current Request Body

Given that the request body in the scenario context map has been reset

Clears the stored request body from the context. Required when a scenario sends multiple requests with different bodies — without resetting, the previous body is reused.

Scenario: Create a resource then fetch it by ID
  Given that the body of the request is
    """
    { "name": "My Resource" }
    """
  When executing an authorized POST call to "/api/v1/resources" with previously given body
  Then I ensure that the status code of the response is 201
  And I store the string of the field "id" in the context "RESOURCE_ID" for later usage

  Given that the request body in the scenario context map has been reset
  When executing an authorized GET call to "/api/v1/resources/${RESOURCE_ID}"
  Then I ensure that the status code of the response is 200
  And I ensure that the body of the response contains a field "name" with the value "My Resource"

Without the reset step, the GET would also carry the POST body, which could cause unexpected behavior.

Clone this wiki locally