API Module¶
The API module provides steps for testing HTTP APIs. It supports three
backends — stdlib (urllib, default), httpx (requires [api]) and
requests — and covers configuration, requests, assertions and response
storage.
Installation¶
pip install "behave-steplib[api]" # httpx backend
pip install "behave-steplib[requests]" # requests backend
The stdlib backend (urllib) is always available with no extra
dependencies. Install the [api] extra to use httpx, or the
[requests] extra to use requests.
Backends¶
Backend |
Package |
Notes |
|---|---|---|
|
(none) |
Default. Uses |
|
|
HTTP/2 support, async-capable, cookie persistence. Requires the
|
|
|
Session-based with cookie persistence. Requires the
|
Select a backend via autoload:
autoload(context, backends={"api": "httpx"})
autoload(context, backends={"api": "requests"})
autoload(context, backends={"api": "stdlib"}) # default, no extra needed
Steps¶
The API module provides 55 steps organised into eight categories. All steps
have es and pt translations unless noted otherwise.
Configuration¶
Pattern |
Description |
|---|---|
|
Set the base URL for subsequent requests. |
|
Set a default header sent with every request. |
|
Set the request timeout in seconds. |
|
Set a default query parameter sent with every request. |
|
Remove a previously set query parameter. |
|
Remove a previously set default header. |
|
Reset headers, params, auth, cookies, and last response. |
|
Set a proxy URL for both HTTP and HTTPS requests. |
Authentication¶
Pattern |
Description |
|---|---|
|
Set basic auth credentials for subsequent requests. |
|
Set a Bearer token in the Authorization header. |
SSL and redirects¶
Pattern |
Description |
|---|---|
|
Disable SSL certificate verification for subsequent requests. |
|
Enable SSL certificate verification for subsequent requests. |
|
Disable following redirects for subsequent requests. |
|
Enable following redirects for subsequent requests. |
|
Extract Set-Cookie headers from the last response and store them. |
Requests¶
Pattern |
Description |
|---|---|
|
Send an HTTP request and store the response. |
|
Send a request with a body from the step’s text ( |
|
Send a request with form-encoded data from the step table. |
|
Send a request with a JSON body from the step’s text. |
|
Send a request with query parameters from the step table. |
|
Send a request with extra headers from the step table. |
The {method} placeholder accepts any HTTP method string (GET,
POST, PUT, PATCH, DELETE, HEAD, OPTIONS). Relative
URLs are resolved against the base URL set with the API base url is.
Status and body assertions¶
Pattern |
Description |
|---|---|
|
Assert the last response status code. |
|
Assert the status is one of a comma-separated list (e.g. |
|
Assert the response body contains a substring. |
|
Assert the response body does not contain a substring. |
|
Assert the response body is valid JSON. |
|
Validate the response body against a JSON Schema from the step text. |
JSON Path assertions¶
Pattern |
Description |
|---|---|
|
Assert a JSON path in the response equals a value. |
|
Assert a JSON path value does not equal a value. |
|
Assert that a JSON path exists in the response. |
|
Assert a JSON path value contains a value (for lists or strings). |
|
Assert that a JSON path value is null. |
|
Assert that a JSON path value is not null. |
|
Assert a JSON path value (list or string) has a specific length. |
|
Assert the value at a JSON path is of a specific type. |
|
Assert a JSON path string value matches a regex pattern. |
Header assertions¶
Pattern |
Description |
|---|---|
|
Assert a response header equals a value. |
|
Assert a response header does not equal a value. |
|
Assert a response header contains a substring. |
|
Assert that a response header exists. |
|
Assert that a response header does not exist. |
|
Assert the Content-Type response header equals a value. |
|
Assert the Content-Type response header contains a substring. |
Response time assertions¶
Pattern |
Description |
|---|---|
|
Assert the response time is under a threshold. |
|
Assert the response time exceeds a threshold. |
|
Assert the response time is within a range. |
Store and extract¶
Pattern |
Description |
|---|---|
|
Store the response body as a named variable. |
|
Store the value at a JSON path from the response as a variable. |
|
Store a response header value as a named variable. |
|
Store the last response status code as a named variable. |
|
Store the last response time in milliseconds as a variable. |
|
Set a header from a previously stored variable. |
|
Set a query parameter from a previously stored variable. |
|
Assert that a stored variable equals a value. |
Table comparison¶
Pattern |
Description |
|---|---|
|
Compare the response JSON with a behave table. Requires the
|
Example¶
Feature: API health check
Scenario: GET users returns 200
Given the API base url is "https://api.example.com"
When I send a GET request to "/users"
Then the response status is 200
And the response body is valid JSON
And the JSON path "$.users[0].name" equals "Ada"
Scenario: POST creates a user
Given the API base url is "https://api.example.com"
When I send a POST request to "/users" with body
"""
{"name": "Ada", "email": "ada@example.com"}
"""
Then the response status is 201
And the response header "Content-Type" is "application/json"
And I store the response body as "created_user"
JSONPath syntax¶
The the JSON path {path} equals {value} step supports a simple JSONPath
subset:
$— the root object.$.field— access a field.$.field.nested— nested access.$.items[0]— array index.
Examples:
Then the JSON path "$" equals "..."
Then the JSON path "$.users" equals "..."
Then the JSON path "$.users[0].name" equals "Ada"
Then the JSON path "$.items[2].price" equals "19.99"
API reference¶
See Modules API Reference for the full autodoc reference of the API module.