Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsJul 18, 2026·3 min de lectura

Behave — Behavior-Driven Development for Python

Behave is a BDD framework for Python that lets you write tests in natural language using Gherkin syntax. Test scenarios are written in plain English .feature files, with step definitions implemented in Python.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Behave BDD
Comando de instalación directa
npx -y tokrepo@latest install c6cb195c-8262-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

Introduction

Behave brings behavior-driven development to Python using the Gherkin language. Product managers and QA engineers write test scenarios in plain English .feature files using Given/When/Then syntax, while developers implement the matching step definitions in Python. This separation keeps specifications readable and tests executable.

What Behave Does

  • Parses Gherkin .feature files written in Given/When/Then natural language syntax
  • Maps Gherkin steps to Python functions via decorator-based step definitions
  • Supports scenario outlines for data-driven testing with example tables
  • Provides hooks (before/after scenario, feature, step) for setup and teardown
  • Generates JUnit XML output for CI integration and custom formatters for reporting

Architecture Overview

Behave reads .feature files from a features directory, parses them into an internal scenario model, and matches each Gherkin step against registered Python step definitions using regex or parse patterns. The context object passes state between steps within a scenario. Environment hooks in environment.py run at feature, scenario, and step boundaries for setup and cleanup.

Self-Hosting & Configuration

  • Install via pip; works with Python 3.7 and later
  • Place .feature files in a features/ directory at the project root
  • Implement step definitions in features/steps/ as Python files
  • Configure behave via behave.ini, .behaverc, or pyproject.toml
  • Use environment.py for shared fixtures, browser setup, or database connections

Key Features

  • Gherkin syntax that non-technical stakeholders can read and validate
  • Step definition reuse across multiple features and scenarios
  • Scenario outlines with Examples tables for parameterized testing
  • Tag-based filtering to run smoke, regression, or WIP test subsets
  • Active formatters for JUnit XML, JSON, progress bars, and custom output

Comparison with Similar Tools

  • pytest-bdd — BDD plugin for pytest; Behave is a standalone framework with its own runner
  • Cucumber — The original BDD framework (Ruby/Java); Behave is the Python equivalent
  • Robot Framework — Keyword-driven; Behave uses Gherkin natural language syntax
  • Lettuce — Older Python BDD tool; Behave is actively maintained with a larger community
  • pytest — Code-centric assertions; Behave separates specification from implementation

FAQ

Q: Can Behave run alongside pytest? A: Yes. You can use pytest-behave or simply run behave as a separate command in your CI pipeline alongside pytest.

Q: Does it support async step definitions? A: Behave's step runner is synchronous. For async code, use asyncio.run() within step definitions or consider pytest-bdd with pytest-asyncio.

Q: How do I share state between steps? A: Use the context object. It is passed to every step function and persists within a scenario. Context attributes set in one step are available in subsequent steps.

Q: Can I generate HTML reports? A: Yes. Use the --format json output with tools like Allure or behave-html-formatter to produce rich HTML reports.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados