Skip to content

Step Libraries

behave-gen ships with two built-in step libraries that generate real, runnable step definitions — not empty skeletons. See ADR-0001 for the rationale.

HTTP step library

behave-gen add steps --lib http

Creates features/steps/http_steps.py with HTTP request steps using Python's standard-library urllib — no extra runtime dependencies.

Available steps

Keyword Pattern Description
Given the base URL is "{url}" Set the base URL for subsequent requests.
When I send a {method} request to "{path}" Send an HTTP request (GET, POST, PUT, DELETE, PATCH).
When I send a {method} request to "{path}" with body Send an HTTP request with a docstring body.
Then the response status should be {status:d} Assert the response status code.
Then the response JSON should contain "{key}" Assert the response JSON has a key.
Then the response JSON at "{key}" should be "{value}" Assert a JSON path value.

Example

Feature: API
  Scenario: List items
    Given the base URL is "http://localhost:8080"
    When I send a GET request to "/items"
    Then the response status should be 200
    And the response JSON should contain "data"

  Scenario: Create item
    Given the base URL is "http://localhost:8080"
    When I send a POST request to "/items" with body
      """
      {"name": "widget"}
      """
    Then the response status should be 201
    And the response JSON at "name" should be "widget"

Configuration

Edit DEFAULT_BASE_URL at the top of http_steps.py to change the default:

DEFAULT_BASE_URL = "http://localhost:8080"
DEFAULT_TIMEOUT = 10

Auth step library

behave-gen add steps --lib auth

Creates features/steps/auth_steps.py with session and authentication state management steps.

Available steps

Keyword Pattern Description
Given I am not authenticated Reset the session to unauthenticated.
Given I have a session token "{token}" Set a session token.
When I store the value "{value}" as "{name}" Store a value in the session.
When I clear the session Clear all session state.
Then I should be authenticated Assert the session is authenticated.
Then I should not be authenticated Assert the session is not authenticated.
Then the session value "{name}" should be "{value}" Assert a stored session value.

Example

Feature: Auth
  Scenario: Login flow
    Given I am not authenticated
    When I store the value "abc123" as "session_token"
    Then I should be authenticated
    And the session value "session_token" should be "abc123"

  Scenario: Logout
    Given I have a session token "abc123"
    When I clear the session
    Then I should not be authenticated

Combining HTTP and auth

The two libraries are designed to work together. Use auth steps to manage session state and HTTP steps to make requests:

@smoke @auth
Feature: Login
  Scenario: Successful login
    Given the base URL is "http://localhost:8080"
    And I am not authenticated
    When I send a POST request to "/auth/login" with body
      """
      {"username": "admin", "password": "secret"}
      """
    Then the response status should be 200
    And the response JSON should contain "token"
    When I store the value "abc123" as "session_token"
    Then I should be authenticated

Updating step libraries

When a new version of behave-gen ships updates to the step libraries, use behave-gen update to refresh them:

behave-gen update --force

Files containing the Generated by behave-gen marker are overwritten. Files without the marker (user-modified) are skipped unless --force is used.