AgentSkillsCN

Acceptance Test Generator

验收测试生成器

SKILL.md

Acceptance Test Generator

Goal: Generate executable BDD acceptance tests from user story acceptance criteria

Description

Converts Given-When-Then acceptance criteria from user stories into executable test files using BDD frameworks (pytest-bdd for Python, Cucumber for JavaScript). Generates feature files, step definitions, and test fixtures.

Usage

  • "Generate acceptance tests from user stories"
  • "Create BDD tests for [stories]"
  • "Write feature files for acceptance criteria"

When to Use

  • After story-writer generates user stories
  • Before implementing features (TDD workflow)
  • Need executable tests from acceptance criteria

Pipeline Contract (sdlc-tdd-full.lobster)

Inputs:

  • stories (JSON array): User stories from story-writer with acceptance_criteria
  • workspace (string): Path to write test files

Output: JSON with structure:

json
{
  "status": "success",
  "test_files": [
    {
      "path": "/opt/nexus/v2/features/profile_api.feature",
      "scenarios_count": 3,
      "type": "feature"
    },
    {
      "path": "/opt/nexus/v2/features/steps/profile_steps.py",
      "step_definitions": 8,
      "type": "step_definitions"
    }
  ],
  "framework": "pytest-bdd",
  "tests_compile": true,
  "tests_pass": false,
  "red_state_verified": true
}

Implementation

Execute in order. Do not run Lobster or any pipeline.

Step 1: Read Stories and Acceptance Criteria

  • Parse stories JSON array
  • Extract all acceptance_criteria (Given-When-Then scenarios)
  • Group scenarios by story for organization

Step 2: Detect Project Language

  • Scan workspace for language indicators:
    • Python: requirements.txt, pyproject.toml, setup.py
    • JavaScript/TypeScript: package.json, tsconfig.json
  • If both found, prioritize based on more recent files
  • Default to Python if unclear

Step 3: Generate Feature Files (Gherkin)

For each story, create features/story_<id>_<slug>.feature:

gherkin
Feature: User can retrieve profile via API
  As a Mobile app user
  I want to retrieve my profile information
  So that I can view and edit my profile data

  Scenario: Authenticated user retrieves profile
    Given User is authenticated with valid JWT token
    And profile exists in database
    When User sends GET request to /api/v1/profile
    Then API returns 200 OK
    And response body contains user_id, name, email, avatar_url

  Scenario: Unauthenticated user tries to access profile
    Given User is not authenticated
    When User sends GET request to /api/v1/profile
    Then API returns 401 Unauthorized

Step 4: Generate Step Definitions

Create step definition files with implementations:

Python (pytest-bdd):

python
# features/steps/profile_steps.py
from pytest_bdd import scenarios, given, when, then, parsers
import pytest

scenarios('../profile_api.feature')

@given('User is authenticated with valid JWT token')
def authenticated_user(test_client):
    # TODO: Set up auth token in test client
    pass

@when(parsers.parse('User sends GET request to {endpoint}'))
def send_get_request(test_client, endpoint):
    # TODO: Make request
    pass

@then(parsers.parse('API returns {status_code}'))
def check_status_code(test_client, status_code):
    # TODO: Assert status code
    pass

JavaScript (Cucumber):

javascript
// features/steps/profile.steps.ts
import { Given, When, Then } from '@cucumber/cucumber';

Given('User is authenticated with valid JWT token', async function() {
  // TODO: Set up auth token
});

When('User sends GET request to {string}', async function(endpoint: string) {
  // TODO: Make request
});

Then('API returns {int}', function(statusCode: number) {
  // TODO: Assert status code
});

Step 5: Add Fixtures and Test Setup

  • Python: Create conftest.py with test_client, database fixtures
  • JavaScript: Create cucumber.config.js or jest.config.js
  • Add helper functions for common setup (auth, database cleanup)

Example conftest.py:

python
import pytest
from app import create_app

@pytest.fixture
def test_client():
    app = create_app('testing')
    with app.test_client() as client:
        yield client

@pytest.fixture
def authenticated_user(test_client):
    # Create test user and return auth token
    pass

Step 6: Verify Tests Compile

  • Python: Run pytest --collect-only features/
  • JavaScript: Run npm test -- --listTests or cucumber-js --dry-run
  • Check for syntax errors, import errors
  • Set tests_compile: true if no errors

Step 7: Run Tests (Verify RED State)

  • Run full test suite to verify tests FAIL as expected (no implementation yet)
  • Python: pytest features/ --tb=short
  • JavaScript: npm test features/
  • Set tests_pass: false and red_state_verified: true
  • This confirms TDD RED state before implementation

Step 8: Output Test File Metadata

Generate JSON with:

  • test_files array (path, scenarios_count, type)
  • framework detected
  • compilation status
  • test pass/fail status (should be false in RED state)

Output Format

Markdown summary + JSON block for pipeline consumption:

markdown
# Acceptance Tests Generated

Framework: **pytest-bdd** (Python)

I've created **3 feature files** with **9 scenarios**:

**features/story_001_profile_api.feature**
├─ Scenarios: 3
├─ Step definitions: 8
└─ Status: ✅ Compiles | ❌ Fails (RED state verified)

**features/story_002_update_profile.feature**
├─ Scenarios: 4
├─ Step definitions: 12
└─ Status: ✅ Compiles | ❌ Fails (RED state verified)

**features/story_003_admin_profiles.feature**
├─ Scenarios: 2
├─ Step definitions: 6
└─ Status: ✅ Compiles | ❌ Fails (RED state verified)

📊 **Summary**: 3 feature files | 9 scenarios | 26 step definitions

🔴 **TDD Status**: RED (tests fail as expected - no implementation yet)

```json
{
  "status": "success",
  "test_files": [
    {"path": "features/story_001_profile_api.feature", "scenarios_count": 3, "type": "feature"},
    {"path": "features/steps/profile_steps.py", "step_definitions": 8, "type": "step_definitions"}
  ],
  "framework": "pytest-bdd",
  "tests_compile": true,
  "tests_pass": false,
  "red_state_verified": true
}
code

## Notes
- Always verify RED state (tests fail before implementation)
- Use TODO comments in step definitions to guide implementation
- Keep step definitions reusable across scenarios
- Add clear assertion messages for debugging
- Include setup/teardown for database, auth, mocks