tdd-guide

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

TDD Guide

TDD 指南

Test-driven development skill for generating tests, analyzing coverage, and guiding red-green-refactor workflows across Jest, Pytest, JUnit, and Vitest.
这是一项测试驱动开发技能,可用于生成测试、分析覆盖率,并在Jest、Pytest、JUnit和Vitest等框架中指导红-绿-重构工作流。

Table of Contents

目录

Capabilities

功能特性

CapabilityDescription
Test GenerationConvert requirements or code into test cases with proper structure
Coverage AnalysisParse LCOV/JSON/XML reports, identify gaps, prioritize fixes
TDD WorkflowGuide red-green-refactor cycles with validation
Framework AdaptersGenerate tests for Jest, Pytest, JUnit, Vitest, Mocha
Quality ScoringAssess test isolation, assertions, naming, detect test smells
Fixture GenerationCreate realistic test data, mocks, and factories

功能特性描述
测试生成将需求或代码转换为结构规范的测试用例
覆盖率分析解析LCOV/JSON/XML报告,识别缺口,确定修复优先级
TDD工作流通过验证指导红-绿-重构循环
框架适配为Jest、Pytest、JUnit、Vitest、Mocha生成测试
质量评分评估测试隔离性、断言、命名,检测测试异味
测试夹具生成创建真实的测试数据、模拟对象和工厂类

Workflows

工作流

Generate Tests from Code

从代码生成测试

  1. Provide source code (TypeScript, JavaScript, Python, Java)
  2. Specify target framework (Jest, Pytest, JUnit, Vitest)
  3. Run
    test_generator.py
    with requirements
  4. Review generated test stubs
  5. Validation: Tests compile and cover happy path, error cases, edge cases
  1. 提供源代码(TypeScript、JavaScript、Python、Java)
  2. 指定目标框架(Jest、Pytest、JUnit、Vitest)
  3. 结合需求运行
    test_generator.py
  4. 审查生成的测试桩代码
  5. 验证: 测试可编译,且覆盖正常路径、错误场景和边缘情况

Analyze Coverage Gaps

分析覆盖率缺口

  1. Generate coverage report from test runner (
    npm test -- --coverage
    )
  2. Run
    coverage_analyzer.py
    on LCOV/JSON/XML report
  3. Review prioritized gaps (P0/P1/P2)
  4. Generate missing tests for uncovered paths
  5. Validation: Coverage meets target threshold (typically 80%+)
  1. 从测试运行器生成覆盖率报告(
    npm test -- --coverage
  2. 对LCOV/JSON/XML报告运行
    coverage_analyzer.py
  3. 查看按优先级排序的缺口(P0/P1/P2)
  4. 为未覆盖路径生成缺失的测试
  5. 验证: 覆盖率达到目标阈值(通常为80%以上)

TDD New Feature

TDD 新功能开发

  1. Write failing test first (RED)
  2. Run
    tdd_workflow.py --phase red
    to validate
  3. Implement minimal code to pass (GREEN)
  4. Run
    tdd_workflow.py --phase green
    to validate
  5. Refactor while keeping tests green (REFACTOR)
  6. Validation: All tests pass after each cycle

  1. 先编写失败的测试(RED阶段)
  2. 运行
    tdd_workflow.py --phase red
    进行验证
  3. 实现最简代码使测试通过(GREEN阶段)
  4. 运行
    tdd_workflow.py --phase green
    进行验证
  5. 在保持测试通过的同时重构代码(REFACTOR阶段)
  6. 验证: 每个循环后所有测试均通过

Tools

工具

ToolPurposeUsage
test_generator.py
Generate test cases from code/requirements
python scripts/test_generator.py --input source.py --framework pytest
coverage_analyzer.py
Parse and analyze coverage reports
python scripts/coverage_analyzer.py --report lcov.info --threshold 80
tdd_workflow.py
Guide red-green-refactor cycles
python scripts/tdd_workflow.py --phase red --test test_auth.py
framework_adapter.py
Convert tests between frameworks
python scripts/framework_adapter.py --from jest --to pytest
fixture_generator.py
Generate test data and mocks
python scripts/fixture_generator.py --entity User --count 5
metrics_calculator.py
Calculate test quality metrics
python scripts/metrics_calculator.py --tests tests/
format_detector.py
Detect language and framework
python scripts/format_detector.py --file source.ts
output_formatter.py
Format output for CLI/desktop/CI
python scripts/output_formatter.py --format markdown

工具用途使用方式
test_generator.py
从代码/需求生成测试用例
python scripts/test_generator.py --input source.py --framework pytest
coverage_analyzer.py
解析并分析覆盖率报告
python scripts/coverage_analyzer.py --report lcov.info --threshold 80
tdd_workflow.py
指导红-绿-重构循环
python scripts/tdd_workflow.py --phase red --test test_auth.py
framework_adapter.py
在不同框架间转换测试
python scripts/framework_adapter.py --from jest --to pytest
fixture_generator.py
生成测试数据和模拟对象
python scripts/fixture_generator.py --entity User --count 5
metrics_calculator.py
计算测试质量指标
python scripts/metrics_calculator.py --tests tests/
format_detector.py
检测语言和框架
python scripts/format_detector.py --file source.ts
output_formatter.py
为CLI/桌面/CI格式化输出
python scripts/output_formatter.py --format markdown

Input Requirements

输入要求

For Test Generation:
  • Source code (file path or pasted content)
  • Target framework (Jest, Pytest, JUnit, Vitest)
  • Coverage scope (unit, integration, edge cases)
For Coverage Analysis:
  • Coverage report file (LCOV, JSON, or XML format)
  • Optional: Source code for context
  • Optional: Target threshold percentage
For TDD Workflow:
  • Feature requirements or user story
  • Current phase (RED, GREEN, REFACTOR)
  • Test code and implementation status

测试生成所需:
  • 源代码(文件路径或粘贴内容)
  • 目标框架(Jest、Pytest、JUnit、Vitest)
  • 覆盖范围(单元测试、集成测试、边缘情况)
覆盖率分析所需:
  • 覆盖率报告文件(LCOV、JSON或XML格式)
  • 可选:用于上下文参考的源代码
  • 可选:目标阈值百分比
TDD工作流所需:
  • 功能需求或用户故事
  • 当前阶段(RED、GREEN、REFACTOR)
  • 测试代码和实现状态

Limitations

局限性

ScopeDetails
Unit test focusIntegration and E2E tests require different patterns
Static analysisCannot execute tests or measure runtime behavior
Language supportBest for TypeScript, JavaScript, Python, Java
Report formatsLCOV, JSON, XML only; other formats need conversion
Generated testsProvide scaffolding; require human review for complex logic
When to use other tools:
  • E2E testing: Playwright, Cypress, Selenium
  • Performance testing: k6, JMeter, Locust
  • Security testing: OWASP ZAP, Burp Suite
范围详情
聚焦单元测试集成测试和E2E测试需要不同的模式
静态分析无法执行测试或衡量运行时行为
语言支持对TypeScript、JavaScript、Python、Java支持最佳
报告格式仅支持LCOV、JSON、XML;其他格式需要转换
生成的测试提供基础框架;复杂逻辑需要人工审查
何时使用其他工具:
  • E2E测试:Playwright、Cypress、Selenium
  • 性能测试:k6、JMeter、Locust
  • 安全测试:OWASP ZAP、Burp Suite