test-updates

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Table of Contents

目录

Test Updates and Maintenance

测试更新与维护

Overview

概述

detailed test management system that applies TDD/BDD principles to maintain, generate, and enhance tests across codebases. This skill practices what it preaches - it uses TDD principles for its own development and serves as a living example of best practices.
这是一个遵循TDD/BDD原则的精细化测试管理系统,用于在代码库中维护、生成和优化测试。该技能以身作则——它自身的开发就采用了TDD原则,是最佳实践的鲜活示例。

Core Philosophy

核心理念

  • RED-GREEN-REFACTOR: Strict adherence to TDD cycle
  • Behavior-First: BDD patterns that describe what code should do
  • Meta Dogfooding: The skill's own tests demonstrate the principles it teaches
  • Quality Gates: detailed validation before considering tests complete
  • RED-GREEN-REFACTOR(红-绿-重构):严格遵循TDD周期
  • 行为优先:采用BDD模式描述代码应实现的功能
  • 自我验证:该技能自身的测试展示了它所传授的原则
  • 质量关卡:在认定测试完成前进行精细化验证

What It Is

功能介绍

A modular test management system that:
  • Discovers what needs testing or updating
  • Generates tests following TDD principles
  • Enhances existing tests with BDD patterns
  • Validate test quality through multiple lenses
这是一个模块化的测试管理系统,具备以下能力:
  • 发现需要测试或更新的内容
  • 遵循TDD原则生成测试
  • 用BDD模式优化现有测试
  • 从多个维度验证测试质量

Quick Start

快速开始

Quick Checklist for First Time Use

首次使用快速检查清单

  • validate pytest is installed (
    pip install pytest
    )
  • Have your source code in
    src/
    or similar directory
  • Create a
    tests/
    directory if it doesn't exist
  • Run
    Skill(sanctum:git-workspace-review)
    first to understand changes
  • Start with
    Skill(test-updates) --target <specific-module>
    for focused updates
  • 确认已安装pytest(执行
    pip install pytest
  • 源代码放在
    src/
    或类似目录下
  • 若不存在
    tests/
    目录则创建它
  • 先运行
    Skill(sanctum:git-workspace-review)
    了解代码变更
  • 执行
    Skill(test-updates) --target <特定模块>
    开始定向更新

detailed Test Update

全面测试更新

bash
undefined
bash
undefined

Run full test update workflow

运行完整的测试更新工作流

Skill(test-updates)
**Verification:** Run `pytest -v` to verify tests pass.
Skill(test-updates)
**验证方式:** 执行`pytest -v`确认测试通过。

Targeted Test Updates

定向测试更新

bash
undefined
bash
undefined

Update tests for specific paths

更新特定路径下的测试

Skill(test-updates) --target src/sanctum/agents Skill(test-updates) --target tests/test_commit_messages.py
**Verification:** Run `pytest -v` to verify tests pass.
Skill(test-updates) --target src/sanctum/agents Skill(test-updates) --target tests/test_commit_messages.py
**验证方式:** 执行`pytest -v`确认测试通过。

TDD for New Features

新功能的TDD实践

bash
undefined
bash
undefined

Apply TDD to new code

对新代码应用TDD

Skill(test-updates) --tdd-only --target new_feature.py
**Verification:** Run `pytest -v` to verify tests pass.
Skill(test-updates) --tdd-only --target new_feature.py
**验证方式:** 执行`pytest -v`确认测试通过。

Using the Scripts Directly

直接使用脚本

Human-Readable Output:
bash
undefined
人类可读输出:
bash
undefined

Analyze test coverage gaps

分析测试覆盖缺口

python plugins/sanctum/skills/test-updates/scripts/test_analyzer.py --scan src/
python plugins/sanctum/skills/test-updates/scripts/test_analyzer.py --scan src/

Generate test scaffolding

生成测试脚手架

python plugins/sanctum/skills/test-updates/scripts/test_generator.py
--source src/my_module.py --style pytest_bdd
python plugins/sanctum/skills/test-updates/scripts/test_generator.py
--source src/my_module.py --style pytest_bdd

Check test quality

检查测试质量

python plugins/sanctum/skills/test-updates/scripts/quality_checker.py
--validate tests/test_my_module.py
**Verification:** Run `pytest -v` to verify tests pass.

**Programmatic Output (for Claude Code):**
```bash
python plugins/sanctum/skills/test-updates/scripts/quality_checker.py
--validate tests/test_my_module.py
**验证方式:** 执行`pytest -v`确认测试通过。

**供Claude Code使用的程序化输出:**
```bash

Get JSON output for programmatic parsing - test_analyzer

获取用于程序化解析的JSON输出 - test_analyzer

python plugins/sanctum/skills/test-updates/scripts/test_analyzer.py
--scan src/ --output-json
python plugins/sanctum/skills/test-updates/scripts/test_analyzer.py
--scan src/ --output-json

Returns:

返回结果:

{

{

"success": true,

"success": true,

"data": {

"data": {

"source_files": ["src/module.py", ...],

"source_files": ["src/module.py", ...],

"test_files": ["tests/test_module.py", ...],

"test_files": ["tests/test_module.py", ...],

"uncovered_files": ["module_without_tests", ...],

"uncovered_files": ["module_without_tests", ...],

"coverage_gaps": [{"file": "...", "reason": "..."}]

"coverage_gaps": [{"file": "...", "reason": "..."}]

}

}

}

}

Get JSON output - test_generator

获取JSON输出 - test_generator

python plugins/sanctum/skills/test-updates/scripts/test_generator.py
--source src/my_module.py --output-json
python plugins/sanctum/skills/test-updates/scripts/test_generator.py
--source src/my_module.py --output-json

Returns:

返回结果:

{

{

"success": true,

"success": true,

"data": {

"data": {

"test_file": "path/to/test_my_module.py",

"test_file": "path/to/test_my_module.py",

"source_file": "src/my_module.py",

"source_file": "src/my_module.py",

"style": "pytest_bdd",

"style": "pytest_bdd",

"fixtures_included": true,

"fixtures_included": true,

"edge_cases_included": true,

"edge_cases_included": true,

"error_cases_included": true

"error_cases_included": true

}

}

}

}

Get JSON output - quality_checker

获取JSON输出 - quality_checker

python plugins/sanctum/skills/test-updates/scripts/quality_checker.py
--validate tests/test_my_module.py --output-json
python plugins/sanctum/skills/test-updates/scripts/quality_checker.py
--validate tests/test_my_module.py --output-json

Returns:

返回结果:

{

{

"success": true,

"success": true,

"data": {

"data": {

"static_analysis": {...},

"static_analysis": {...},

"dynamic_validation": {...},

"dynamic_validation": {...},

"metrics": {...},

"metrics": {...},

"quality_score": 85,

"quality_score": 85,

"quality_level": "QualityLevel.GOOD",

"quality_level": "QualityLevel.GOOD",

"recommendations": [...]

"recommendations": [...]

}

}

}

}

**Verification:** Run `pytest -v` to verify tests pass.
**验证方式:** 执行`pytest -v`确认测试通过。

When To Use It

适用场景

Use this skill when you need to:
  • Update tests after code changes
  • Generate tests for new features
  • Improve existing test quality
  • validate detailed test coverage
Perfect for:
  • Pre-commit test validation
  • CI/CD pipeline integration
  • Refactoring with test safety
  • Onboarding new developers
在以下场景中使用该技能:
  • 代码变更后更新测试
  • 为新功能生成测试
  • 提升现有测试质量
  • 验证测试的全面覆盖
尤其适合:
  • 提交前的测试验证
  • CI/CD流水线集成
  • 借助测试安全地重构代码
  • 新开发者入职培训

When NOT To Use

不适用场景

  • Auditing test suites - use pensive:test-review
  • Writing production code
    • focus on implementation first
  • Auditing test suites - use pensive:test-review
  • Writing production code
    • focus on implementation first
  • 审计测试套件——请使用pensive:test-review
  • 编写生产代码——请先专注于实现
  • 审计测试套件——请使用pensive:test-review
  • 编写生产代码——请先专注于实现

Workflow Integration

工作流集成

Phase 1: Discovery

阶段1:发现

  1. Scan codebase for test gaps
  2. Analyze recent changes
  3. Identify broken or outdated tests
See
modules/test-discovery.md
for detection patterns.
  1. 扫描代码库寻找测试缺口
  2. 分析近期代码变更
  3. 识别失效或过时的测试
详见
modules/test-discovery.md
中的检测模式。

Phase 2: Strategy

阶段2:策略

  1. Choose appropriate BDD style (see
    modules/bdd-patterns.md
    )
  2. Plan test structure
  3. Define quality criteria
  1. 选择合适的BDD风格(详见
    modules/bdd-patterns.md
  2. 规划测试结构
  3. 定义质量标准

Phase 3: Implementation

阶段3:实现

  1. Write failing tests (RED) - see
    modules/tdd-workflow.md
  2. Implement minimal passing code (GREEN)
  3. Refactor for clarity (REFACTOR)
See
modules/test-generation.md
for generation templates.
  1. 编写失败的测试(红阶段)——详见
    modules/tdd-workflow.md
  2. 实现最小化的可通过代码(绿阶段)
  3. 重构代码以提升可读性(重构阶段)
详见
modules/test-generation.md
中的生成模板。

Phase 4: Validation

阶段4:验证

  1. Static analysis and linting
  2. Dynamic test execution
  3. Coverage and quality metrics
See
modules/quality-validation.md
for validation criteria.
  1. 静态分析与代码检查
  2. 动态测试执行
  3. 覆盖度与质量指标
详见
modules/quality-validation.md
中的验证标准。

Quality Assurance

质量保障

The skill applies multiple quality checks:
  • Static: Linting, type checking, pattern validation
  • Dynamic: Test execution in sandboxed environments
  • Metrics: Coverage, mutation score, complexity analysis
  • Review: Structured checklists for peer validation
该技能会应用多维度的质量检查:
  • 静态检查:代码检查、类型检查、模式验证
  • 动态检查:在沙箱环境中执行测试
  • 指标分析:覆盖度、变异测试分数、复杂度分析
  • 同行评审:结构化的清单供同行验证

Examples

示例

BDD-Style Test Generation

BDD风格测试生成

See
modules/bdd-patterns.md
for additional patterns.
python
class TestGitWorkflow:
    """BDD-style tests for Git workflow operations."""

    def test_commit_workflow_with_staged_changes(self):
        """
        GIVEN a Git repository with staged changes
        WHEN the user runs the commit workflow
        THEN it should create a commit with proper message format
        AND all tests should pass
        """
        # Test implementation following TDD principles
        pass
Verification: Run
pytest -v
to verify tests pass.
更多模式详见
modules/bdd-patterns.md
python
class TestGitWorkflow:
    """针对Git工作流操作的BDD风格测试。"""

    def test_commit_workflow_with_staged_changes(self):
        """
        GIVEN 一个包含已暂存变更的Git仓库
        WHEN 用户运行提交工作流
        THEN 应创建一个符合规范格式的提交
        AND 所有测试均应通过
        """
        # 遵循TDD原则的测试实现
        pass
验证方式: 执行
pytest -v
确认测试通过。

Test Enhancement

测试优化

  • Add edge cases and error scenarios
  • Include performance benchmarks
  • Add mutation testing for robustness
See
modules/test-enhancement.md
for enhancement strategies.
  • 添加边缘场景和错误场景
  • 包含性能基准测试
  • 增加变异测试以提升鲁棒性
优化策略详见
modules/test-enhancement.md

Integration with Existing Skills

与现有技能集成

  1. git-workspace-review: Get context of changes
  2. file-analysis: Understand code structure
  3. test-driven-development: Apply strict TDD discipline
  4. skills-eval: Validate quality and compliance
  1. git-workspace-review:获取代码变更上下文
  2. file-analysis:理解代码结构
  3. test-driven-development:严格遵循TDD规范
  4. skills-eval:验证质量与合规性

Success Metrics

成功指标

  • Test coverage > 85%
  • All tests follow BDD patterns
  • Zero broken tests in CI
  • Mutation score > 80%
  • 测试覆盖度 > 85%
  • 所有测试均遵循BDD模式
  • CI流水线中无失效测试
  • 变异测试分数 > 80%

Troubleshooting FAQ

故障排查FAQ

Common Issues

常见问题

Q: Tests are failing after generation A: This is expected! The skill follows TDD principles - generated tests are designed to fail first. Follow the RED-GREEN-REFACTOR cycle:
  1. Run the test and confirm it fails for the right reason
  2. Implement minimal code to make it pass
  3. Refactor for clarity
Q: Quality score is low despite having tests A: Check for these common issues:
  • Missing BDD patterns (Given/When/Then)
  • Vague assertions like
    assert result is not None
  • Tests without documentation
  • Long, complex tests (>50 lines)
Q: Generated tests don't match my code structure A: The scripts analyze AST patterns and may need guidance:
  • Use
    --style
    flag to match your preferred BDD style
  • Check that source files have proper function/class definitions
  • Review the generated scaffolding and customize as needed
Q: Mutation testing takes too long A: Mutation testing is resource-intensive:
  • Use
    --quick-mutation
    flag for subset testing
  • Focus on critical modules first
  • Run overnight for detailed analysis
Q: Can't find tests for my file A: The analyzer uses naming conventions:
  • Source:
    my_module.py
    → Test:
    test_my_module.py
  • Check that test files follow pytest naming patterns
  • validate test directory structure is standard
问:生成测试后测试失败 答:这是正常现象!该技能遵循TDD原则——生成的测试设计为首先失败。请遵循RED-GREEN-REFACTOR周期:
  1. 运行测试并确认它因正确的原因失败
  2. 实现最小化代码使测试通过
  3. 重构代码以提升可读性
问:已有测试但质量分数较低 答:请检查以下常见问题:
  • 缺少BDD模式(Given/When/Then)
  • 模糊的断言,如
    assert result is not None
  • 测试无文档说明
  • 冗长复杂的测试(超过50行)
问:生成的测试与我的代码结构不匹配 答:脚本会分析AST模式,可能需要一些引导:
  • 使用
    --style
    参数匹配你偏好的BDD风格
  • 确保源文件有正确的函数/类定义
  • 查看生成的脚手架并按需自定义
问:变异测试耗时过长 答:变异测试资源消耗较大:
  • 使用
    --quick-mutation
    参数进行子集测试
  • 先聚焦于核心模块
  • 运行整夜以进行精细化分析
问:找不到对应文件的测试 答:分析器使用命名约定:
  • 源文件:
    my_module.py
    → 测试文件:
    test_my_module.py
  • 确保测试文件遵循pytest命名模式
  • 确认测试目录结构符合标准

Performance Tips

性能优化技巧

  • Large codebases: Use
    --target
    to focus on specific directories
  • CI integration: Run validation in parallel with other checks
  • Memory usage: Process files in batches for very large projects
  • 大型代码库:使用
    --target
    参数聚焦于特定目录
  • CI集成:与其他检查并行运行验证
  • 内存使用:处理超大型项目时分批处理文件

Getting Help

获取帮助

  1. Check script outputs for detailed error messages
  2. Use
    --verbose
    flag for more information
  3. Review the validation report for specific recommendations
  4. Start with small modules to understand patterns before scaling
  1. 查看脚本输出获取详细错误信息
  2. 使用
    --verbose
    参数获取更多信息
  3. 查看验证报告获取具体建议
  4. 先从小模块入手理解模式,再进行规模化使用