behave (BDD Framework)

1.3.3 · active · verified Thu Apr 09

behave is a behavior-driven development (BDD) framework for Python, enabling teams to write executable specifications in Gherkin feature files. It supports Gherkin v6, Cucumber-Expressions, and async-steps, allowing for clear, human-readable tests. The current stable version is 1.3.3, with releases typically focusing on bug fixes and incremental feature enhancements, sometimes with pre-releases for larger changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple feature file and corresponding step definitions using behave. It covers defining Gherkin steps with parameter parsing and basic assertions. Organize your files as shown, then run `behave` from the project root.

# Create the following file structure in your project root:
#
# ./
# ├── features/
# │   ├── example.feature
# │   └── steps/
# │       └── example_steps.py
# └── (run 'behave' from this root directory)

# --- features/example.feature ---
# Feature: Basic addition
#   As a calculator user
#   I want to be able to add numbers
#   So that I can get the sum
#
#   Scenario: Add two numbers
#     Given I have the numbers 5 and 3
#     When I add them
#     Then the result should be 8

# --- features/steps/example_steps.py ---
from behave import given, when, then

@given('I have the numbers {num1:d} and {num2:d}')
def step_impl(context, num1, num2):
    context.num1 = num1
    context.num2 = num2

@when('I add them')
def step_impl(context):
    context.result = context.num1 + context.num2

@then('the result should be {expected_result:d}')
def step_impl(context, expected_result):
    assert context.result == expected_result

# To run this example:
# 1. Create the files as shown above.
# 2. Navigate to your project's root directory (containing the 'features' folder) in your terminal.
# 3. Execute the behave command:
#    $ behave

view raw JSON →