Intermediate
Chapter 14 · 8 min read
Doc Strings & Multi-line Data
Use doc strings for multi-line text input in Gherkin — JSON payloads, email bodies, long text, and content verification.
Doc Strings — Multi-line Content
Doc strings (triple-quoted blocks) pass multi-line text to a step. They're ideal for JSON payloads, email content, error messages, or any block of text that would be awkward on one line.
docstring.feature
Feature: API Integration via Cucumber
Scenario: Create a user via API
When the API receives a POST request to "/users" with body:
"""json
{
"name": "Prasanna",
"email": "pv@example.com",
"role": "QA Engineer"
}
"""
Then the response status should be 201
Scenario: Verify email content
When a welcome email is sent
Then the email body should contain:
"""
Dear Prasanna,
Welcome to our platform! Your account has been created.
Best regards,
The Team
"""
Scenario: Verify error log
When the application crashes
Then the error log should contain:
"""
ERROR: NullPointerException at Line 42
Stack trace available in debug mode
"""
DocStringSteps.java
package com.bdd.course.stepdefinitions;
import io.cucumber.java.en.*;
public class DocStringSteps {
@When("the API receives a POST request to {string} with body:")
public void apiPostWithBody(String endpoint, String body) {
System.out.println("POST " + endpoint);
System.out.println("Body: " + body);
// Parse JSON and make API call
}
@Then("the response status should be {int}")
public void responseStatus(int status) {
System.out.println("Expected status: " + status);
}
@When("a welcome email is sent")
public void sendEmail() { System.out.println("Email sent"); }
@Then("the email body should contain:")
public void emailBodyContains(String expectedBody) {
System.out.println("Expected body:\n" + expectedBody);
}
@When("the application crashes")
public void appCrash() {}
@Then("the error log should contain:")
public void errorLogContains(String expectedLog) {
System.out.println("Expected log:\n" + expectedLog);
}
}
docstring.feature
Feature: API Integration via Cucumber
Scenario: Create a user via API
When the API receives a POST request to "/users" with body:
"""json
{
"name": "Prasanna",
"email": "pv@example.com",
"role": "QA Engineer"
}
"""
Then the response status should be 201
Scenario: Verify email content
When a welcome email is sent
Then the email body should contain:
"""
Dear Prasanna,
Welcome to our platform!
"""
docstring_steps.py
# features/steps/docstring_steps.py
from behave import when, then
import json
@when('the API receives a POST request to "{endpoint}" with body')
def step_api_post(context, endpoint):
body = context.text # Doc string is in context.text
print(f"POST {endpoint}")
print(f"Body: {body}")
# Parse if JSON
data = json.loads(body)
print(f"Name: {data['name']}")
@then('the response status should be {status:d}')
def step_response_status(context, status):
print(f"Expected status: {status}")
@when('a welcome email is sent')
def step_send_email(context):
print("Email sent")
@then('the email body should contain')
def step_email_body(context):
expected = context.text
print(f"Expected body:\n{expected}")
Cucumber BDD
Intermediate
Doc Strings & Multi-line Data
Written by PV
© 2026 All Rights Reserved