Lettuce vs Behave: Python BDD Framework Comparison

Lettuce vs Behave: Python BDD Framework Comparison

Both Lettuce and Behave let you write BDD tests in Python using Gherkin feature files. They share the same basic model: .feature files with Given/When/Then steps, Python functions that match steps via decorators, and a test runner that ties them together. The differences matter when you're picking one for a real project.

Quick Overview

Lettuce was released in 2010, modeled after Ruby's Cucumber. It has first-class Django support through the harvest management command and lettuce.django app. Development has slowed significantly since around 2015, and the PyPI package sees infrequent updates.

Behave launched in 2012 and has stayed more actively maintained. It has a richer plugin ecosystem, better documentation, more formatter options, and broader framework support. It is the de facto standard Python BDD framework as of 2024.

Feature Comparison

Feature Lettuce Behave
Python version support 2.x / limited 3.x 3.6+
Django integration Built-in (harvest) Via behave-django plugin
Step decorator @step(regex) @given, @when, @then
Shared state world object context object passed to steps
Hooks @before.each_scenario before_scenario(context, scenario)
Formatters Limited JSON, JUnit, progress, pretty
Active maintenance Minimal Active
Tags Yes Yes, with --tags logic
Parallel execution No built-in Via behave-parallel

Step Definition Style

Lettuce uses a single @step() decorator that matches Given, When, and Then steps with one regex:

# Lettuce
from lettuce import step, world

@step('I add (\d+) and (\d+)')
def add_numbers(step, a, b):
    world.result = int(a) + int(b)

Behave uses separate decorators for each keyword and passes a context object:

# Behave
from behave import when

@when('I add {a:d} and {b:d}')
def add_numbers(context, a, b):
    context.result = a + b

Behave's {a:d} syntax uses parse format strings instead of raw regex, which is more readable for simple cases. You can still use full regex with @step in Behave if needed.

The context object in Behave is functionally equivalent to Lettuce's world, but it is passed explicitly to every step function rather than imported as a global. This makes dependencies between steps explicit and easier to trace.

Django Integration

Lettuce's Django support is built-in. Add lettuce.django to INSTALLED_APPS and run python manage.py harvest. No additional packages needed.

Behave requires behave-django:

pip install behave-django

Add it to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'behave_django',
]

Then run tests with:

python manage.py behave

Both approaches give you access to the ORM, test client, and transaction rollback between scenarios. behave-django is actively maintained and supports newer Django versions, which is a practical advantage.

Hooks Syntax

Lettuce hooks use decorators:

from lettuce import before, after

@before.each_scenario
def setup(scenario):
    world.client = TestClient()

@after.all
def teardown():
    cleanup()

Behave hooks live in an environment.py file with named functions:

# features/environment.py

def before_scenario(context, scenario):
    context.client = Client()

def after_all(context):
    cleanup()

The Behave approach keeps all hooks in one place, which is easier to maintain when the hook file grows.

Reporting

Lettuce's output options are limited. You can get JUnit XML with --with-xunit:

lettuce --with-xunit --xunit-file=results.xml

Behave ships with multiple formatters:

behave --format json -o results.json
behave --format junit --outdir reports/
behave --format progress  # compact progress bar

For CI dashboards that consume test reports, Behave's JUnit output is more complete and widely supported.

When to Use Lettuce

  • You are maintaining an existing Lettuce test suite and the cost of migration outweighs the benefits.
  • You are working on a Python 2 codebase (rare, but Lettuce supports it).
  • Your team is already familiar with it and the project scope is small.

Do not start a new project with Lettuce in 2024. The maintenance gap is real, and Python 3 compatibility has rough edges.

When to Use Behave

  • Starting a new BDD test suite in Python.
  • You need reliable Django integration with active maintenance.
  • Your CI requires structured output formats (JSON, JUnit).
  • The team is likely to grow, and you want better documentation and community support.

Migrating from Lettuce to Behave

The core concepts map directly. The mechanical changes are:

  1. Replace from lettuce import step, world with from behave import given, when, then
  2. Add context as the first argument to all step functions
  3. Replace world.foo with context.foo
  4. Move hook functions into features/environment.py
  5. Replace python manage.py harvest with python manage.py behave (after adding behave-django)

Feature files themselves do not change. Gherkin is Gherkin, and both frameworks parse the same format.

If your codebase has hundreds of step definitions, do the migration incrementally: run both runners in parallel during a transition period, migrating one feature file at a time.

The fastest way to evaluate Behave on an existing project is to pick one small feature file, port its steps, and run both suites in CI for a sprint before committing to the full migration.

Read more

Start now free