Lettuce Python BDD: Getting Started Guide

Lettuce Python BDD: Getting Started Guide

Lettuce is a BDD framework for Python inspired by Ruby's Cucumber. It reads plain-English Gherkin feature files and maps each step to a Python function. The result is a test suite that non-developers can read and that developers can actually run.

This guide covers installation, your first feature file, step definitions, and running the test suite.

Installation

Lettuce is on PyPI. Install it into your project's virtual environment:

pip install lettuce

If you're working with Django, add the Django integration:

pip install lettuce django

Verify the install:

lettuce --version

Project Layout

Lettuce looks for a features/ directory by default. Inside it, you put .feature files and a steps/ subdirectory for your Python step definitions:

myproject/
  features/
    calculator.feature
    steps/
      calculator_steps.py

You can override the features path with lettuce path/to/features.

Writing a Feature File

Feature files use Gherkin syntax: Feature, Scenario, Given, When, Then, and And. Here's a minimal example:

# features/calculator.feature

Feature: Basic calculator operations
  As a developer
  I want to test arithmetic
  So I can trust the logic layer

  Scenario: Adding two numbers
    Given I have a calculator
    When I add 3 and 5
    Then the result should be 8

  Scenario: Subtracting numbers
    Given I have a calculator
    When I subtract 2 from 10
    Then the result should be 8

Each line starting with Given, When, Then, or And is a step. Lettuce matches each step to a Python function using a regex.

Writing Step Definitions

Step definitions live in Python files inside features/steps/. Use the @step() decorator from lettuce and pass a regex that matches the step text:

# features/steps/calculator_steps.py

from lettuce import step, world

class Calculator:
    def __init__(self):
        self.result = 0

    def add(self, a, b):
        self.result = a + b

    def subtract(self, a, b):
        self.result = b - a


@step('I have a calculator')
def have_a_calculator(step):
    world.calc = Calculator()


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


@step('I subtract (\d+) from (\d+)')
def subtract_numbers(step, a, b):
    world.calc.subtract(int(a), int(b))


@step('the result should be (\d+)')
def check_result(step, expected):
    assert world.calc.result == int(expected), \
        f"Expected {expected}, got {world.calc.result}"

The world object is a shared namespace scoped to each scenario run. It's the standard way to pass state between steps.

Running Tests

From the project root:

lettuce

Lettuce discovers features/ automatically. You'll see colored output showing which steps passed or failed:

Feature: Basic calculator operations

  Scenario: Adding two numbers
    Given I have a calculator          # passed
    When I add 3 and 5                 # passed
    Then the result should be 8        # passed

  Scenario: Subtracting numbers
    Given I have a calculator          # passed
    When I subtract 2 from 10          # passed
    Then the result should be 8        # passed

1 feature (1 passed)
2 scenarios (2 passed)
6 steps (6 passed)

To run a specific feature file:

lettuce features/calculator.feature

To run a specific scenario by line number:

lettuce features/calculator.feature:6

Scenario Outlines

When the same scenario logic applies to multiple input sets, use Scenario Outline with an Examples table instead of duplicating scenarios:

  Scenario Outline: Adding various numbers
    Given I have a calculator
    When I add <a> and <b>
    Then the result should be <result>

    Examples:
      | a  | b  | result |
      | 1  | 2  | 3      |
      | 10 | 20 | 30     |
      | 0  | 0  | 0      |

Lettuce expands this into three separate scenarios at runtime. The step definitions receive the values as strings, so cast them as needed.

Background Steps

If every scenario in a feature needs the same setup, use Background instead of repeating the Given in each scenario:

Feature: Calculator operations

  Background:
    Given I have a calculator

  Scenario: Addition
    When I add 4 and 6
    Then the result should be 10

Tags

You can tag scenarios and run only tagged subsets:

  @smoke
  Scenario: Adding two numbers
    ...
lettuce --tags=smoke

This is useful for separating fast smoke tests from slower integration scenarios.

Common Pitfall

Lettuce step matching is regex-based and case-sensitive by default. If a step isn't matching, add --verbosity=4 to see which steps are being attempted:

lettuce --verbosity=4

This prints the regex being tried for each step, which makes mismatches obvious immediately.

The next thing to tackle after getting basic steps working is organizing reusable steps into separate files and wiring up before/after hooks for shared setup and teardown. That's covered in the step definitions patterns post.

Read more

Start now free