python-behave-examples

July 1, 2026 · View on GitHub

Repository with examples of use of the Behave library in Python.

This project is a complete, self-contained Behave automation suite that demonstrates every major Gherkin / Behave feature using behave 1.3.x (latest release: 1.3.3).

What's inside

Feature fileDemonstrates
features/calculator.featureBackground, Scenario Outline with multiple Examples tables, tags
features/string_utils.featureGherkin v6 Rule blocks, Example keyword, Background inside a Rule
features/shopping_cart.featureData tables, DocStrings ("""), Scenario Outline with tables
features/async_steps.featureAsync step definitions (behave 1.3.x native support)
features/api/users_api.featureREST API testing with requests against a real Flask server, data tables, pagination, Scenario Outline
features/api/csv_examples.featureExternal CSV file as Examples source

Behave capabilities showcased

  • Gherkin v6 grammar: Rule, Example, Background (including inside Rules)
  • Scenario Outline with multiple Examples tables
  • Data tables (step-level) and DocStrings
  • Tags (@smoke, @negative, @api, @unit, @integration, @wip) and tag filtering
  • Async steps (async def step functions)
  • Custom type converters (register_type)
  • Environment hooks: before_all / after_all, before_feature / after_feature, before_rule / after_rule, before_scenario / after_scenario, before_step / after_step
  • context.add_cleanup for stack-based teardown
  • External CSV Examples tables
  • In-memory Flask SUT started in a background thread (no external services needed)
  • Multiple report formats: pretty, JSON, JUnit XML, HTML, Markdown, Cucumber JSON, step catalogs — all via configurable formatters in behave.ini

Project structure

python-behave-examples/
├── behave.ini                      # Behave configuration (formats, outfiles, junit)
├── requirements.txt                # Python dependencies
├── README.md
├── reports/                        # Generated reports (created on run)
│   ├── pretty.txt                  # Behave built-in pretty formatter
│   ├── results.json                # Behave built-in JSON formatter
│   ├── behave_modern_html_report.html  # Modern HTML report (behave-modern-html-report)
│   ├── step_catalog.html           # HTML step catalog
│   ├── results_rjson.json          # Modern JSON report (behave-modern-json-report)
│   ├── results_cucumber.json       # Cucumber JSON format (CI-compatible)
│   ├── results_markdown.md         # Markdown report (behave-modern-md-report)
│   ├── step_catalog.md             # Markdown step catalog
│   └── junit/                      # JUnit XML reports (one file per feature)
└── features/
    ├── environment.py              # Lifecycle hooks (before_all, before_rule, ...)

    ├── support/                    # Shared support code (SUT + domain models)
    │   ├── __init__.py
    │   ├── app.py                  # Flask SUT (in-memory REST API)
    │   ├── domain.py               # Calculator, StringUtils, ShoppingCart, async helpers
    │   └── data/
    │       └── users.csv           # External Examples data (CSV)

    ├── calculator/                 # Domain: calculator
    │   └── calculator.feature      # Background + Scenario Outline

    ├── string_utils/               # Domain: string utilities
    │   └── string_utils.feature    # Gherkin v6 Rule blocks

    ├── shopping_cart/              # Domain: shopping cart
    │   └── shopping_cart.feature   # Data tables + DocStrings

    ├── async/                      # Domain: async steps
    │   └── async_steps.feature     # async def step definitions

    ├── api/                        # Domain: REST API
    │   ├── users_api.feature       # CRUD testing with requests
    │   └── csv_examples.feature    # External CSV Examples

    └── steps/                      # Step definitions (auto-discovered by behave)
        ├── common_steps.py         # Shared steps + register_type
        ├── calculator_steps.py
        ├── string_utils_steps.py
        ├── shopping_cart_steps.py
        ├── async_steps.py
        └── api_steps.py

Setup

pip install -r requirements.txt

Running tests

# Run everything — reports are auto-generated in reports/ via behave.ini
# (pretty.txt, results.json, junit/*.xml)
behave

# Run only smoke tests
behave --tags=@smoke

# Run only API integration tests
behave --tags=@api

# Tag expression: smoke tests that are not negative
behave --tags="@smoke and not @negative"

# Run a single feature file
behave features/calculator/calculator.feature

Report configuration

All outputs are configured in behave.ini using the format and outfiles multi-line keys (paired by position). Custom formatters are registered in the [behave.formatters] section.

File formatters (paired with outfiles)

Formatter nameLibraryOutput fileDescription
prettybehave (built-in)reports/pretty.txtHuman-readable colored output
jsonbehave (built-in)reports/results.jsonBehave JSON summary
modernbehave-modern-html-reportreports/behave_modern_html_report.htmlModern interactive HTML report
stepsbehave-modern-html-reportreports/step_catalog.htmlHTML step catalog (all registered steps)
rjsonbehave-modern-json-reportreports/results_rjson.jsonEnhanced JSON with metadata (project, branch, build)
cucumberbehave-modern-json-reportreports/results_cucumber.jsonCucumber-compatible JSON (CI tools like Jenkins)
markdownbehave-modern-md-reportreports/results_markdown.mdMarkdown report for documentation/GitHub
stepcatalogbehave-modern-md-reportreports/step_catalog.mdMarkdown step catalog
(junit)behave (built-in)reports/junit/*.xmlJUnit XML — enabled via junit = true

Console formatters (default_format)

The console output is controlled by default_format in behave.ini. Available console formatters (registered in [behave.formatters]):

Formatter nameLibraryDescription
minimalbehave-modern-console-reportCompact one-line-per-scenario output
progressbehave-modern-console-reportProgress bar with percentage
modernbehave-modern-console-reportModern colored output with timestamps
modern-livebehave-modern-console-reportLive-updating modern output
logbehave-modern-console-reportStructured log format with timestamps
cibehave-modern-console-reportCI-optimized output
prettybehave (built-in)Default behave pretty output

To switch the console format, change default_format in behave.ini:

default_format = progress

Adding a new report library

  1. Install the package: pip install <package>
  2. Register the formatter in [behave.formatters]:
    [behave.formatters]
    myformat = my_package.formatter:MyFormatter
    
  3. Add it to the format / outfiles lists:
    format = pretty
        json
        myformat
    outfiles = reports/pretty.txt
        reports/results.json
        reports/my_report.html
    

Requirements

  • Python 3.10+
  • behave 1.3.3
  • Flask, requests, PyHamcrest, jsonschema
  • behave-modern-html-report 2.2.1
  • behave-modern-json-report 1.1.0
  • behave-modern-md-report 1.2.0
  • behave-modern-console-report 1.0.1