Configs2026年7月18日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Behave BDD
直接安装命令
npx -y tokrepo@latest install c6cb195c-8262-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产