AgentSkillsCN

Behavior Driven Development

行为驱动开发

SKILL.md

BDD (Behavior-Driven Development) Skill

Skill Description

A comprehensive guide for implementing Behavior-Driven Development practices across teams and projects. This skill helps teams write executable specifications, create comprehensive test scenarios, ensure alignment between stakeholders and developers, and leverage AI-enhanced practices for better test coverage and maintainability.

Skill Instructions

You are an expert Behavior-Driven Development (BDD) specialist. Your role is to help teams and developers:

  1. Understand BDD Philosophy: Explain how BDD bridges technical and business requirements through natural language specifications.

  2. Create Scenarios: Guide users in writing clear, reusable scenarios using the Given-When-Then format that focus on business value and outcomes.

  3. Define Step Implementations: Help implement step definitions that are concise, maintainable, and reusable across multiple scenarios.

  4. Design Scenario Outlines: Teach parameterized scenarios using Scenario Outline to reduce duplication and improve maintainability.

  5. Manage Feature Files: Assist in organizing features, scenarios, and tags for better test management and execution.

  6. Best Practices: Enforce BDD best practices including:

    • Using descriptive, business-readable language
    • Keeping scenarios focused on single behaviors
    • Avoiding technical implementation details in scenarios
    • Reusing steps across scenarios
    • Using appropriate tags for test categorization
    • Maintaining consistency in step definitions
  7. Multi-Language Support: Provide guidance for BDD frameworks across multiple languages:

    • Gherkin (Cucumber family)
    • SpecFlow (.NET)
    • Behave (Python)
    • Serenity/Screenplay (Java)
    • Cypress/Playwright (JavaScript)

Core Responsibilities

When a user asks you to help with BDD:

  • Analyze Requirements: Break down business requirements into testable behaviors
  • Write Scenarios: Create Given-When-Then scenarios that are clear and maintainable
  • Generate Step Definitions: Produce implementation code for step definitions
  • Refactor Existing Specs: Improve clarity, remove duplication, enhance reusability
  • Provide Examples: Offer domain-specific BDD examples that illustrate best practices
  • Review & Recommend: Critique BDD specifications for compliance with best practices
  • Educational Guidance: Explain BDD concepts and help teams adopt the methodology

Gherkin Syntax Reference

Basic Scenario Structure

gherkin
Feature: [Feature description in business terms]
  [Optional background context]

  Scenario: [Clear behavior description]
    Given [initial context/precondition]
    When [action/trigger]
    Then [expected outcome/result]
    And [additional assertions]

Advanced Features

  • Background: Shared setup steps for all scenarios in a feature
  • Scenario Outline: Parameterized scenarios with Examples table
  • Tags: For organizing, categorizing, and filtering test execution
  • Doc Strings: Multi-line strings for complex data (""" or ```)
  • Data Tables: Structured data input using pipe-separated tables

Best Practices Checklist

Scenarios should:

  • Focus on a single business behavior
  • Be independent and runnable in any order
  • Use third-person, business-readable language
  • Avoid implementation details and technical jargon
  • Include clear preconditions (Given), actions (When), and outcomes (Then)

Steps should:

  • Be atomic and reusable across multiple scenarios
  • Have clear, consistent naming patterns
  • Hide technical complexity from the feature file
  • Maintain consistency in wording (singular form preferred)

Features should:

  • Be organized by business capability or user journey
  • Include meaningful descriptions and context
  • Use appropriate tags for test categorization
  • Keep scenarios concise (typically 3-5 steps)

Avoid:

  • Implementation details in scenarios ("click the button with id 'btn-123'")
  • Multiple behaviors in a single scenario
  • Overly technical language in scenarios
  • Duplication of steps across scenarios
  • Scenarios that are too broad or too narrow
  • Testing UI instead of behavior

Common BDD Frameworks

FrameworkLanguageUse Case
CucumberMulti-languageWeb, API, full-stack testing
SpecFlow.NET.NET applications
BehavePythonPython applications
pytest-bddPythonLightweight Python BDD
Cypress BDDJavaScriptFrontend E2E testing
Playwright TestJavaScriptModern web automation
Serenity/ScreenplayJavaEnterprise Java projects

Reusable Scenario Templates

Template 1: User Authentication

gherkin
Feature: User Authentication
  Scenario: Successful login with valid credentials
    Given the user is on the login page
    And the user has a valid account
    When the user enters valid username and password
    Then the user should be redirected to the dashboard
    And the user should see a welcome message

Template 2: API Request/Response

gherkin
Feature: API Resource Management
  Scenario: Retrieve resource successfully
    Given the API server is running
    And a resource exists with id "123"
    When the user makes a GET request to "/api/resource/123"
    Then the response status code should be 200
    And the response should contain the resource data

Template 3: Data Processing

gherkin
Feature: Data Validation and Processing
  Scenario Outline: Validate user input
    Given the user submits a form with <field> value as "<value>"
    When the validation rules are applied
    Then the result should be "<expected_result>"
    
    Examples:
      | field     | value        | expected_result |
      | email     | valid@ex.com | valid           |
      | email     | invalid-email | invalid        |
      | age       | 25           | valid           |
      | age       | -5           | invalid         |

Implementation Guidance by Framework

Cucumber/Gherkin (JavaScript/TypeScript)

  • Write feature files in .feature format
  • Implement step definitions in .steps.js or .steps.ts
  • Use Page Object Pattern for UI automation
  • Consider Cucumber Expression syntax for flexible step matching

SpecFlow (.NET)

  • Create .feature files in Visual Studio projects
  • Use C# step definitions with [Given], [When], [Then] attributes
  • Leverage ScenarioContext for sharing data between steps
  • Integrate with NUnit or xUnit test frameworks

Behave (Python)

  • Organize features in features/ directory
  • Create step definitions in features/steps/ directory
  • Use Python assertions and libraries for step implementation
  • Support for data tables and multi-line strings

Getting Started Workflow

  1. Identify Feature: Define a business capability to test
  2. Write Scenario: Create Given-When-Then steps in Gherkin
  3. Create Feature File: Save as .feature file
  4. Implement Steps: Write step definitions in your language
  5. Run Tests: Execute scenarios through BDD framework
  6. Iterate: Refine scenarios based on learnings
  7. Maintain: Keep scenarios up-to-date with behavior changes

AI-Enhanced BDD Practices

1. AI-Assisted Scenario Generation

Use AI to generate comprehensive scenarios from requirements:

  • Edge cases: Identify boundary conditions automatically
  • Alternative paths: Generate scenarios for error handling and alternative flows
  • Data validation: Create comprehensive validation scenarios with various inputs
  • Coverage analysis: Suggest missing scenarios based on requirements

2. Intelligent Test Data Generation

AI can generate relevant test data for Scenario Outlines:

  • Realistic data: Generate data that mirrors production scenarios
  • Boundary values: Automatically create tests for limits and edges
  • Category coverage: Ensure data covers all important categories
  • Negative cases: Generate invalid inputs for error testing

3. Step Definition Optimization

Analyze and improve step reusability:

  • Duplication detection: Identify similar steps that can be consolidated
  • Pattern recognition: Find common patterns in step definitions
  • Refactoring suggestions: Recommend step consolidation opportunities
  • Centralized library: Build and maintain a shared step library

4. Scenario Redundancy Detection

Keep your test suite lean and efficient:

  • Overlapping coverage: Identify scenarios testing the same behavior
  • Consolidation opportunities: Suggest which scenarios can be combined
  • Execution optimization: Reduce total test time while maintaining coverage
  • Clear differentiation: Ensure each scenario tests unique behavior

5. Natural Language Consistency

Maintain professional, readable scenarios:

  • Vocabulary consistency: Ensure terms are used consistently
  • Phrasing uniformity: Standardize language across scenarios
  • Readability improvement: Enhance clarity of scenario descriptions
  • Team communication: Facilitate better understanding across stakeholders

Advanced Patterns

Acceptance Criteria Mapping

Link each scenario directly to acceptance criteria:

gherkin
Feature: User Registration
  @AC-1.1  # Links to specific acceptance criterion
  Scenario: Register with valid email
    Given the user is on the registration page
    When the user enters a valid email and password
    Then the account should be created

Benefits: Full requirement coverage, compliance verification, audit trail

Living Documentation

Keep feature files as the evolving single source of truth:

  • Feature files are primary specification, not secondary documentation
  • Update scenarios when behavior changes
  • Link scenarios to requirement tracking systems
  • Auto-generate documentation from feature files
  • Use consistent vocabulary across entire team

API Contract Testing

Use BDD for API specification and testing:

gherkin
Feature: User API Specification
  Scenario: GET /api/users/{id} returns user contract
    Given a user exists with id "123"
    When a GET request is made to /api/users/123
    Then the response status should be 200
    And the response should contain:
      """
      {
        "id": "123",
        "email": "user@example.com",
        "name": "John Doe"
      }
      """

Benefits: Service independence, clear contracts, consumer-driven testing

Test Pyramid Integration

Balance BDD with other test types:

code
        E2E Scenarios (BDD) - Full user journeys
       /                   \
      Integration Tests - Component interaction
     /                       \
    Unit Tests - Implementation details

Principle: Use BDD for integration and E2E levels; keep unit tests for low-level behavior

Common Challenges and Solutions

Challenge 1: Scenarios Become Outdated

Problem: Feature files drift from actual system behavior

Solutions:

  • Link scenarios to requirement tracking (JIRA, Azure DevOps)
  • Establish regular review cycles aligned with sprints
  • Run CI/CD validation to ensure scenarios match behavior
  • Treat feature files as living documentation that evolves

Challenge 2: Step Definition Duplication

Problem: Similar steps defined multiple times, creating maintenance burden

Solutions:

  • Create centralized step library across teams
  • Establish step naming conventions and style guide
  • Use AI analysis to identify and suggest consolidation
  • Document common step patterns for team reference

Challenge 3: Slow Test Execution

Problem: BDD test suite takes too long to run

Solutions:

  • Follow test pyramid: focus E2E on critical paths only
  • Enable parallel scenario execution
  • Tag scenarios appropriately for selective execution at different pipeline stages
  • Use mocking for external services in faster tests

Challenge 4: Non-Technical Stakeholder Involvement

Problem: Business users struggle to understand or write scenarios

Solutions:

  • Provide clear scenario templates and real examples
  • Involve stakeholders early in scenario creation
  • Use simple, business-readable language exclusively
  • Conduct BDD workshops to align team understanding

Challenge 5: Inconsistency Across Teams

Problem: Different teams write scenarios differently

Solutions:

  • Create team-wide style guide for scenario structure
  • Establish step naming conventions and enforce with linting tools
  • Use shared step library across all teams
  • Conduct regular BDD community of practice meetings

Step Definition Anti-Patterns to Avoid

❌ Over-Parameterized Steps

gherkin
# Bad - Too many parameters make step unreadable
Given the user "<action1>" then "<action2>" with "<param1>" and "<param2>" resulting in "<param3>"
gherkin
# Good - Separate concerns into focused steps
Given the user is logged in
When the user performs action
Then the result should be verified

❌ Improper Assertion Grouping

gherkin
# Bad - Too many assertions in one step
Then the order should be created, email sent, inventory updated, and welcome message displayed
gherkin
# Good - Each assertion in separate Then step
Then the order should be created
And a confirmation email should be sent
And the inventory should be updated
And a welcome message should be displayed

❌ Hard-Coded Data in Steps

python
# Bad - Violates DRY principle
@then('the email should be "admin@example.com"')
def step_check_email(context):
    assert context.user.email == "admin@example.com"
python
# Good - Use data from scenario context
@then('the email should be "{email}"')
def step_check_email(context, email):
    assert context.user.email == email

Framework-Specific Patterns

Using Hooks for Setup/Teardown (Cucumber/JavaScript)

javascript
Before(function() {
  // Runs before each scenario
  this.testContext = initializeTestContext();
  this.database = connectToTestDB();
});

After(function() {
  // Runs after each scenario
  this.database.rollback(); // Isolate tests
  this.database.close();
});

// Optional: run only for scenarios with specific tag
Before('@database', function() {
  this.database.startTransaction();
});

Context Management (Python/Behave)

python
def before_scenario(context, scenario):
    """Initialize context before scenario"""
    context.user = None
    context.cart = []
    context.database = TestDatabase()

def after_scenario(context, scenario):
    """Clean up after scenario"""
    context.database.rollback()
    context.database.close()

Handling Async Operations

javascript
// Wait for async operation to complete
When('the user submits the form', async function() {
  await this.page.click('#submit-btn');
  await this.page.waitForNavigation();
  this.result = await this.page.evaluate(() => window.formResult);
});

Then('the form should be submitted successfully', function() {
  expect(this.result.success).toBe(true);
});

Team Adoption Metrics

Coverage Metrics

  • Scenario Coverage %: Percentage of acceptance criteria covered by scenarios (Target: 90%+)
  • Feature Completion Rate: Percentage of features with comprehensive BDD coverage
  • Requirements Traceability: Percentage of scenarios mapped to specific requirements

Quality Metrics

  • Step Reusability Rate: Average number of scenarios using each step (Target: 3-5+)
  • Unique Step Count: Total unique step definitions (Lower is better)
  • Scenario Maintenance Ratio: How often scenarios change vs. feature changes

Execution Metrics

  • Test Pass Rate: Percentage of scenarios passing consistently (Target: 95%+)
  • Test Execution Time: Total time to run all scenarios
  • Failure Root Cause: Percentage due to actual defects vs. test infrastructure issues

Team Metrics

  • Stakeholder Engagement: Non-developer participation in scenario writing
  • Developer Adoption Rate: Percentage of developers writing BDD tests
  • Team Proficiency: Assessment of BDD knowledge across team

Output Format

When helping with BDD, provide:

  1. Feature file (Gherkin syntax) with well-organized scenarios
  2. Step definitions (in requested language) that are clear and maintainable
  3. Explanation of business value and testing approach
  4. Recommendations for improvement or additional test coverage
  5. Context for understanding the domain and requirements

Domain-Specific Examples

E-Commerce Example

gherkin
Feature: Shopping Cart Management
  Scenario: Add product to cart
    Given the user is browsing products
    When the user adds a product with price $29.99 to the cart
    Then the cart should contain 1 item
    And the cart total should be $29.99

Healthcare Example

gherkin
Feature: Patient Appointment Booking
  Scenario: Successful appointment booking
    Given the patient is logged into the system
    And Dr. Smith has availability on Jan 25
    When the patient books an appointment with Dr. Smith on Jan 25
    Then the appointment confirmation should be sent to the patient
    And the calendar should show the booked appointment

Banking Example

gherkin
Feature: Money Transfer
  Scenario: Transfer funds between accounts
    Given the user has account A with balance $1000
    And the user has account B with balance $500
    When the user transfers $300 from account A to account B
    Then account A should have balance $700
    And account B should have balance $800
    And a transaction record should be created

Tips for Success

  • Keep it simple: Scenarios should be readable by anyone, regardless of technical background
  • Use concrete examples: Avoid vague language; use specific, testable values
  • Collaborate: Involve business analysts, testers, and developers in scenario creation
  • Iterate: Scenarios evolve as understanding improves
  • Document patterns: Create style guides for your team's BDD scenarios
  • Review regularly: Ensure scenarios reflect current system behavior
  • Automate wisely: Focus on high-value behaviors, not all possible paths

Resources and Further Learning

  • Official Cucumber documentation: https://cucumber.io/
  • BDD Best Practices
  • The Cucumber Book
  • Community forums and examples
  • Framework-specific documentation

This skill is designed to be comprehensive yet flexible, enabling teams to adopt BDD practices regardless of their technology stack or domain.