test-debug-failures

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Debug Test Failures

调试测试失败

When to Use This Skill

何时使用该技能

Use this skill when users mention:
  • "tests are failing"
  • "pytest errors"
  • "test suite not passing"
  • "debug test failures"
  • "fix broken tests"
  • "tests failing after changes"
  • "mock not called"
  • "assertion error in tests"
  • Any test-related debugging task
当用户提及以下内容时使用本技能:
  • "测试失败"
  • "pytest错误"
  • "测试套件未通过"
  • "调试测试失败"
  • "修复损坏的测试"
  • "代码变更后测试失败"
  • "Mock未被调用"
  • "测试中出现断言错误"
  • 任何与测试相关的调试任务

What This Skill Does

该技能的作用

Provides systematic evidence-based test debugging through 6 mandatory phases:
  1. Evidence Collection - Run tests with verbose output, capture actual errors
  2. Root Cause Analysis - Categorize problems (test setup vs business logic vs integration)
  3. Specific Issue Location - Identify exact file:line:column for ALL occurrences
  4. Systematic Fix - Plan and implement fixes for all related errors
  5. Validation - Re-run tests and verify no regressions
  6. Quality Gates - Run project-specific checks (type, lint, dead code)
Prevents assumption-driven fixes by enforcing proper diagnostic sequence.
通过6个强制阶段提供系统性的、基于证据的测试调试:
  1. 证据收集 - 以详细输出模式运行测试,捕获实际错误信息
  2. 根本原因分析 - 对问题进行分类(测试设置问题 vs 业务逻辑问题 vs 集成问题)
  3. 具体问题定位 - 为所有错误 occurrence 标记确切的文件:行:列位置
  4. 系统性修复 - 规划并实施所有相关错误的修复方案
  5. 验证 - 重新运行测试,确认无回归问题
  6. 质量门禁 - 运行项目特定检查(类型检查、代码 lint、死代码检测等)
通过强制遵循规范的诊断流程,避免基于假设的随意修复。

Quick Start

快速开始

When tests fail, use this skill to systematically identify and fix root causes:
bash
undefined
当测试失败时,使用本技能系统性地识别并修复根本原因:
bash
undefined

This skill will guide you through:

本技能将引导您完成以下步骤:

1. Running tests with verbose output

1. 以详细输出模式运行测试

2. Parsing actual error messages

2. 解析实际错误信息

3. Identifying root cause (test setup vs business logic vs integration)

3. 识别根本原因(测试设置/业务逻辑/集成问题)

4. Locating specific issues (file:line:column)

4. 定位具体问题(文件:行:列)

5. Fixing ALL occurrences systematically

5. 系统性修复所有相关错误

6. Validating the fix

6. 验证修复效果

7. Running quality gates

7. 运行质量门禁检查

undefined
undefined

Table of Contents

目录

Core Sections

核心章节

Framework-Specific Guides

框架特定指南

  • Framework-Specific Quick Reference - Test runner commands and flags
    • Python (pytest) - Run tests, common flags, debugging options
    • JavaScript/TypeScript (Jest/Vitest) - Jest flags, Vitest reporter options
    • Go - Test commands, race detection, specific package testing
    • Rust - Cargo test, output control, sequential execution
  • 框架特定快速参考 - 测试运行器命令与参数
    • Python (pytest) - 运行测试、常用参数、调试选项
    • JavaScript/TypeScript (Jest/Vitest) - Jest参数、Vitest报告器选项
    • Go - 测试命令、竞态检测、特定包测试
    • Rust - Cargo test、输出控制、顺序执行

Advanced Topics

高级主题

  • 成功标准 - 任务完成检查清单
  • 示例 - 详细演练(见examples.md)
  • 相关文档 - 项目CLAUDE.md、质量门禁、测试策略
  • 理念 - 基于证据的调试原则

Instructions

操作说明

YOU MUST follow this sequence. No shortcuts.
您必须严格遵循此流程,不得跳过任何步骤。

Phase 1: Evidence Collection

阶段1:证据收集

1.1 Run Tests First - See Actual Errors
DO NOT make any changes before running tests. Execute with maximum verbosity:
Python/pytest:
bash
uv run pytest <failing_test_path> -v --tb=short
1.1 先运行测试 - 查看实际错误
在运行测试前请勿进行任何修改,以最大详细度执行测试:
Python/pytest:
bash
uv run pytest <failing_test_path> -v --tb=short

Or for full stack traces:

或获取完整堆栈跟踪:

uv run pytest <failing_test_path> -vv --tb=long
uv run pytest <failing_test_path> -vv --tb=long

Or for specific test function:

或针对特定测试函数:

uv run pytest <file>::<test_function> -vv

**JavaScript/TypeScript:**
```bash
uv run pytest <file>::<test_function> -vv

**JavaScript/TypeScript:**
```bash

Jest

Jest

npm test -- --verbose --no-coverage <test_path>
npm test -- --verbose --no-coverage <test_path>

Vitest

Vitest

npm run test -- --reporter=verbose <test_path>
npm run test -- --reporter=verbose <test_path>

Mocha

Mocha

npm test -- --reporter spec <test_path>

**Other Languages:**
```bash
npm test -- --reporter spec <test_path>

**其他语言:**
```bash

Go

Go

go test -v ./...
go test -v ./...

Rust

Rust

cargo test -- --nocapture --test-threads=1
cargo test -- --nocapture --test-threads=1

Ruby (RSpec)

Ruby (RSpec)

bundle exec rspec <spec_path> --format documentation

**1.2 Capture Output**

Save the COMPLETE output. Do not summarize. Do not assume.

**1.3 Read The Error - Parse Actual Messages**

Identify:
- Error type (AssertionError, AttributeError, ImportError, etc.)
- Error message (exact wording)
- Stack trace (which functions called which)
- Line numbers (where error originated)

**CRITICAL:** Is this a "mock not called" error or an actual assertion failure?
bundle exec rspec <spec_path> --format documentation

**1.2 捕获输出**

保存完整的输出内容,不得总结,不得主观假设。

**1.3 读取错误信息 - 解析实际消息**

识别以下内容:
- 错误类型(AssertionError、AttributeError、ImportError等)
- 错误消息(确切文字)
- 堆栈跟踪(函数调用链)
- 行号(错误起源位置)

**关键:** 这是“Mock未被调用”错误还是实际断言失败?

Phase 2: Root Cause Analysis

阶段2:根本原因分析

2.1 Categorize The Problem
Test Setup Issues:
  • Mock configuration incorrect (
    mock_X not called
    ,
    mock has no attribute Y
    )
  • Fixture problems (missing, incorrectly scoped, teardown issues)
  • Test data issues (invalid inputs, wrong test doubles)
  • Import/dependency injection errors
Business Logic Bugs:
  • Assertion failures on expected values
  • Logic errors in implementation
  • Missing validation
  • Incorrect algorithm
Integration Issues:
  • Database connection failures
  • External service unavailable
  • File system access problems
  • Environment configuration missing
2.2 Check Test vs Code
Use Read tool to examine:
  1. The failing test file
  2. The code being tested
  3. Related fixtures/mocks/setup
CRITICAL QUESTION: Is the problem in how the test is written, or what the code does?
2.1 对问题进行分类
测试设置问题:
  • Mock配置错误(
    mock_X not called
    mock has no attribute Y
  • Fixture问题(缺失、作用域错误、清理问题)
  • 测试数据问题(无效输入、错误的测试替身)
  • 导入/依赖注入错误
业务逻辑Bug:
  • 预期值断言失败
  • 实现中的逻辑错误
  • 缺失验证
  • 算法错误
集成问题:
  • 数据库连接失败
  • 外部服务不可用
  • 文件系统访问问题
  • 环境配置缺失
2.2 检查测试与代码
使用读取工具检查:
  1. 失败的测试文件
  2. 被测试的代码
  3. 相关的fixture/mock/设置代码
关键问题: 问题出在测试的编写方式上,还是代码的实际功能上?

Phase 3: Specific Issue Location

阶段3:具体问题定位

3.1 Provide Exact Locations
For EVERY error, document:
  • File: Absolute path to file
  • Line: Exact line number
  • Column: Character position (if available)
  • Function/Method: Name of failing function
  • Error Type: Specific exception/assertion
  • Actual vs Expected: What was expected, what was received
Example:
File: /abs/path/to/test_service.py
Line: 45
Function: test_process_data
Error: AssertionError: mock_repository.save not called
Expected: save() called once with data={'key': 'value'}
Actual: save() never called
3.2 Identify ALL Occurrences
Use Grep to find all instances of the pattern:
bash
undefined
3.1 提供确切位置
针对每个错误,记录:
  • 文件: 文件的绝对路径
  • 行: 确切行号
  • 列: 字符位置(如果可用)
  • 函数/方法: 失败函数的名称
  • 错误类型: 具体的异常/断言类型
  • 实际值 vs 预期值: 预期结果与实际结果
示例:
文件:/abs/path/to/test_service.py
行:45
函数:test_process_data
错误:AssertionError: mock_repository.save not called
预期:save() 被调用1次,参数为data={'key': 'value'}
实际:save() 从未被调用
3.2 识别所有出现的问题
使用Grep查找所有匹配模式的实例:
bash
undefined

Find all similar test patterns

查找所有类似的测试模式

grep -r "pattern_causing_error" tests/
grep -r "pattern_causing_error" tests/

Find all places where mocked function is used

查找所有使用被Mock函数的位置

grep -r "mock_function_name" tests/

**DO NOT fix just the first occurrence. Fix ALL of them.**
grep -r "mock_function_name" tests/

**请勿仅修复第一个出现的问题,要修复所有问题。**

Phase 4: Systematic Fix

阶段4:系统性修复

4.1 Plan The Fix
Before making changes, document:
  • What needs to change
  • Why this fixes the root cause
  • How many files/locations affected
  • Whether this is test code or business logic
4.2 Implement Fix
Use MultiEdit for multiple changes to same file:
python
undefined
4.1 规划修复方案
在进行修改前,记录:
  • 需要修改的内容
  • 该修复能解决根本原因的理由
  • 涉及的文件/位置数量
  • 是修改测试代码还是业务逻辑
4.2 实施修复
对同一文件的多处修改使用MultiEdit:
python
undefined

✅ CORRECT - All edits to same file in one operation

✅ 正确方式 - 同一文件的所有修改一次完成

MultiEdit("path/to/file.py", [ {"old_string": incorrect_mock_setup_1, "new_string": correct_mock_setup_1}, {"old_string": incorrect_mock_setup_2, "new_string": correct_mock_setup_2}, {"old_string": incorrect_assertion, "new_string": correct_assertion} ])

**For changes across multiple files, use Edit for each file:**

```python
MultiEdit("path/to/file.py", [ {"old_string": incorrect_mock_setup_1, "new_string": correct_mock_setup_1}, {"old_string": incorrect_mock_setup_2, "new_string": correct_mock_setup_2}, {"old_string": incorrect_assertion, "new_string": correct_assertion} ])

**对多个文件的修改,每个文件使用Edit:**

```python

Fix in test file

修复测试文件

Edit("tests/test_service.py", old_string=..., new_string=...)
Edit("tests/test_service.py", old_string=..., new_string=...)

Fix in implementation

修复实现代码

Edit("src/service.py", old_string=..., new_string=...)

**4.3 Address All Related Errors**

If error appears in 10 places, fix all 10. No arbitrary limits.

If pattern appears across files:
1. Use Grep to find all occurrences
2. Document each location
3. Fix each location
4. Track completion
Edit("src/service.py", old_string=..., new_string=...)

**4.3 解决所有相关错误**

如果错误出现在10个位置,就修复全部10处,不得随意限制数量。

如果问题模式跨文件出现:
1. 使用Grep查找所有出现的位置
2. 记录每个位置
3. 修复每个位置
4. 跟踪完成情况

Phase 5: Validation

阶段5:验证

5.1 Re-run Tests
Run the EXACT same test command from Phase 1:
bash
undefined
5.1 重新运行测试
运行与阶段1完全相同的测试命令:
bash
undefined

Same command as before

与之前相同的命令

uv run pytest <failing_test_path> -vv

**5.2 Check Results**

- ✅ **PASS:** All tests green → Proceed to Phase 6
- ❌ **FAIL:** New errors → Return to Phase 2 (different root cause)
- ⚠️ **PARTIAL:** Some pass, some fail → Incomplete fix, return to Phase 3

**5.3 Run Full Test Suite**

Ensure no regressions:

```bash
uv run pytest <failing_test_path> -vv

**5.2 检查结果**

- ✅ **通过:** 所有测试通过 → 进入阶段6
- ❌ **失败:** 出现新错误 → 返回阶段2(不同的根本原因)
- ⚠️ **部分通过:** 部分通过部分失败 → 修复不完整,返回阶段3

**5.3 运行全量测试套件**

确保无回归问题:

```bash

Python

Python

uv run pytest tests/ -v
uv run pytest tests/ -v

JavaScript

JavaScript

npm test
npm test

Go

Go

go test ./...
undefined
go test ./...
undefined

Phase 6: Quality Gates

阶段6:质量门禁

6.1 Run Project-Specific Quality Checks
For this project (project-watch-mcp):
bash
./scripts/check_all.sh
Generic quality gates:
bash
undefined
6.1 运行项目特定质量检查
针对本项目(project-watch-mcp):
bash
./scripts/check_all.sh
通用质量门禁:
bash
undefined

Type checking

类型检查

pyright # or tsc, or mypy
pyright # 或tsc、mypy

Linting

代码Lint

ruff check src/ # or eslint, or rubocop
ruff check src/ # 或eslint、rubocop

Dead code detection

死代码检测

vulture src/ # or knip (JS)
vulture src/ # 或knip(JS)

Security

安全检查

bandit src/ # or npm audit

**6.2 Verify All Gates Pass**

Task is NOT done if quality gates fail. Fix or request guidance.
bandit src/ # 或npm audit

**6.2 验证所有门禁通过**

如果质量门禁未通过,任务未完成。需修复或请求指导。

Usage Examples

使用示例

Example 1: Mock Configuration Issue

示例1:Mock配置问题

bash
undefined
bash
undefined

User says: "Tests failing in test_service.py"

用户反馈:"Tests failing in test_service.py"

Phase 1: Run tests

阶段1:运行测试

uv run pytest tests/test_service.py -vv
uv run pytest tests/test_service.py -vv

Error: AssertionError: Expected 'save' to be called once. Called 0 times.

错误:AssertionError: Expected 'save' to be called once. Called 0 times.

Phase 2: Root cause - Mock not called (execution path issue)

阶段2:根本原因 - Mock未被调用(执行路径问题)

Phase 3: Location - test_service.py:45, function test_process_data

阶段3:位置 - test_service.py:45,函数test_process_data

Phase 4: Fix mock configuration to match actual code path

阶段4:修复Mock配置以匹配实际代码路径

Phase 5: Re-run tests - PASS

阶段5:重新运行测试 - 通过

Phase 6: Run quality gates - PASS

阶段6:运行质量门禁 - 通过

undefined
undefined

Example 2: Business Logic Assertion

示例2:业务逻辑断言失败

bash
undefined
bash
undefined

User says: "test_calculate is failing"

用户反馈:"test_calculate is failing"

Phase 1: Run tests

阶段1:运行测试

uv run pytest tests/test_calculator.py::test_calculate -vv
uv run pytest tests/test_calculator.py::test_calculate -vv

Error: AssertionError: assert 42 == 43

错误:AssertionError: assert 42 == 43

Phase 2: Root cause - Business logic error (calculation off by 1)

阶段2:根本原因 - 业务逻辑错误(计算结果差1)

Phase 3: Location - src/calculator.py:23, function calculate

阶段3:位置 - src/calculator.py:23,函数calculate

Phase 4: Fix algorithm in implementation

阶段4:修复实现中的算法

Phase 5: Re-run tests - PASS

阶段5:重新运行测试 - 通过

Phase 6: Run quality gates - PASS

阶段6:运行质量门禁 - 通过

undefined
undefined

Example 3: Integration Test Failure

示例3:集成测试失败

bash
undefined
bash
undefined

User says: "Integration tests are failing"

用户反馈:"Integration tests are failing"

Phase 1: Run tests

阶段1:运行测试

uv run pytest tests/integration/ -vv
uv run pytest tests/integration/ -vv

Error: Neo4j connection failed

错误:Neo4j connection failed

Phase 2: Root cause - Integration issue (database unavailable)

阶段2:根本原因 - 集成问题(数据库不可用)

Phase 3: Location - Environment configuration

阶段3:位置 - 环境配置

Phase 4: Start Neo4j Desktop

阶段4:启动Neo4j Desktop

Phase 5: Re-run tests - PASS

阶段5:重新运行测试 - 通过

Phase 6: Run quality gates - PASS

阶段6:运行质量门禁 - 通过

undefined
undefined

Validation Process

验证流程

This skill enforces systematic validation at each phase:
  1. Evidence Validation - Actual test output captured, not assumed errors
  2. Root Cause Validation - Problem categorized correctly (setup vs logic vs integration)
  3. Location Validation - Exact file:line:column identified for ALL occurrences
  4. Fix Validation - Re-run tests to verify fix works
  5. Regression Validation - Full test suite passes, no new failures
  6. Quality Validation - All quality gates pass (type check, lint, dead code)
本技能在每个阶段强制进行系统性验证:
  1. 证据验证 - 捕获实际测试输出,而非假设的错误
  2. 根本原因验证 - 问题分类正确(设置/逻辑/集成)
  3. 位置验证 - 所有错误都标记了确切的文件:行:列
  4. 修复验证 - 重新运行测试确认修复有效
  5. 回归验证 - 全量测试套件通过,无新失败
  6. 质量验证 - 所有质量门禁通过

Expected Outcomes

预期结果

Successful Debugging

调试成功

✓ Tests Fixed

Failing tests: tests/test_service.py::test_process_data
Root cause: Mock configuration - save() not called due to early return
Fix applied: Updated mock to handle validation failure path
Location: tests/test_service.py:45-52

Test results:
  ✓ Specific test: PASS
  ✓ Full suite: PASS (no regressions)
  ✓ Quality gates: PASS

Time: 8 minutes (systematic debugging)
Confidence: High (evidence-based fix)
✓ 测试已修复

失败测试:tests/test_service.py::test_process_data
根本原因:Mock配置问题 - 由于提前返回导致save()未被调用
修复内容:更新Mock以处理验证失败路径
位置:tests/test_service.py:45-52

测试结果:
  ✓ 特定测试:通过
  ✓ 全量套件:通过(无回归)
  ✓ 质量门禁:通过

耗时:8分钟(系统性调试)
信心:高(基于证据的修复)

Debugging Failure (Needs More Investigation)

调试失败(需进一步调查)

⚠️ Additional Investigation Required

Failing tests: tests/test_integration.py::test_database
Root cause: Partially identified - connection issue
Current status: Neo4j started, but connection still failing

Next steps:
  1. Check Neo4j logs for startup errors
  2. Verify connection configuration in settings
  3. Test connection manually with cypher-shell
  4. Check firewall/network settings

Blocker: Database configuration needs review
⚠️ 需要进一步调查

失败测试:tests/test_integration.py::test_database
根本原因:部分识别 - 连接问题
当前状态:Neo4j已启动,但连接仍然失败

下一步:
  1. 检查Neo4j启动日志中的错误
  2. 验证设置中的连接配置
  3. 使用cypher-shell手动测试连接
  4. 检查防火墙/网络设置

阻塞点:数据库配置需要审核

Integration Points

集成点

With Project CLAUDE.md

与项目CLAUDE.md集成

Implements CLAUDE.md debugging approach:
  • "Run tests first" - Enforced in Phase 1
  • "Read the error" - Phase 1.3 parses actual messages
  • "Be specific" - Phase 3 requires file:line:column
  • "Be complete" - Phase 4 fixes ALL occurrences
实现CLAUDE.md调试方法:
  • "先运行测试" - 在阶段1强制执行
  • "读取错误信息" - 阶段1.3解析实际消息
  • "具体化" - 阶段3要求提供文件:行:列
  • "完整性" - 阶段4修复所有出现的问题

With Quality Gates

与质量门禁集成

Integrates with quality validation:
  • Phase 6 runs project check_all.sh
  • Ensures type checking passes (pyright)
  • Validates linting (ruff)
  • Checks for dead code (vulture)
与质量验证集成:
  • 阶段6运行项目check_all.sh
  • 确保类型检查通过(pyright)
  • 验证代码Lint通过(ruff)
  • 检查死代码(vulture)

With Other Skills

与其他技能集成

Coordinates with complementary skills:
  • run-quality-gates
    - Automated Phase 6 execution
  • analyze-logs
    - Investigate test failure logs
  • debug-type-errors
    - Resolve type checking failures
  • setup-async-testing
    - Fix async test issues
与互补技能协同:
  • run-quality-gates
    - 自动执行阶段6
  • analyze-logs
    - 调查测试失败日志
  • debug-type-errors
    - 解决类型检查失败
  • setup-async-testing
    - 修复异步测试问题

Expected Benefits

预期收益

MetricWithout SkillWith SkillImprovement
Debug Time30-60 min (trial & error)8-15 min (systematic)4-7x faster
Fix Accuracy~60% (assumptions)~95% (evidence-based)58% improvement
Regressions~20% (incomplete fixes)<2% (full validation)90% reduction
Complete Fixes~40% (first occurrence only)~98% (all occurrences)145% improvement
Quality Gate Pass~70% (skipped)100% (enforced)43% improvement
指标无技能时使用技能后提升幅度
调试时间30-60分钟(反复试错)8-15分钟(系统性)4-7倍提速
修复准确率~60%(基于假设)~95%(基于证据)提升58%
回归问题~20%(修复不完整)<2%(全量验证)减少90%
完整修复率~40%(仅修复第一个问题)~98%(修复所有问题)提升145%
质量门禁通过率~70%(跳过检查)100%(强制通过)提升43%

Success Metrics

成功指标

After using this skill:
  • 100% evidence-based - No assumptions, only actual test output
  • 95% fix accuracy - Root cause identified correctly
  • 98% complete fixes - All occurrences addressed
  • <2% regressions - Full suite validation prevents new failures
  • 100% quality gate compliance - All checks pass before "done"
使用本技能后:
  • 100%基于证据 - 无主观假设,仅基于实际测试输出
  • 95%修复准确率 - 正确识别根本原因
  • 98%完整修复 - 解决所有出现的问题
  • <2%回归问题 - 全量套件验证避免新失败
  • 100%质量门禁合规 - 所有检查通过才标记任务完成

Red Flags to Avoid

需要避免的危险信号

❌ WRONG: Making changes before running tests
"The mock setup looks wrong, let me fix it..."
✅ RIGHT: Run test first, see actual error
"Let me run the test to see the exact error message..."

❌ WRONG: Assuming errors are unrelated
"This ImportError is probably unrelated to the test failure..."
✅ RIGHT: Investigate every error
"Let me trace why this import is failing - it may be the root cause..."

❌ WRONG: Fixing first occurrence only
"Fixed the mock in test_create.py, done!"
✅ RIGHT: Search for all occurrences
"Let me search for this pattern across all test files..."

❌ WRONG: Skipping quality gates
"Tests pass now, we're done!"
✅ RIGHT: Run full validation
"Tests pass. Running quality gates to check for regressions..."

❌ WRONG: Quick fix without understanding
"Let me just add a try/except to suppress this error..."
✅ RIGHT: Understand root cause
"This error indicates a deeper issue. Let me investigate why..."
❌ 错误做法:在运行测试前进行修改
"这个Mock设置看起来不对,我先修改一下..."
✅ 正确做法:先运行测试,查看实际错误
"我先运行测试看看确切的错误消息..."

❌ 错误做法:假设错误无关
"这个ImportError可能和测试失败无关..."
✅ 正确做法:调查每个错误
"这个错误表明存在更深层次的问题,我来调查原因..."

❌ 错误做法:仅修复第一个出现的问题
"修复了test_create.py中的Mock,完成了!"
✅ 正确做法:搜索所有出现的问题
"我来搜索所有测试文件中这个模式的出现位置..."

❌ 错误做法:跳过质量门禁
"测试通过了,我们完成了!"
✅ 正确做法:运行全量验证
"测试通过了,现在运行质量门禁检查是否有回归..."

❌ 错误做法:不理解问题就快速修复
"我加个try/except把这个错误屏蔽掉..."
✅ 正确做法:理解根本原因
"这个错误表明存在更深层次的问题,我来调查原因..."

Common Test Failure Patterns

常见测试失败模式

Pattern 1: Mock Not Called

模式1:Mock未被调用

Error:
AssertionError: Expected 'save' to be called once. Called 0 times.
Root Cause:
  • Code path not executing
  • Early return before mock called
  • Exception preventing execution
  • Wrong mock target
Investigation:
  1. Read the test - what's being tested?
  2. Read the implementation - is save() actually called?
  3. Check for early returns or exceptions
  4. Verify mock is patching correct location
错误:
AssertionError: Expected 'save' to be called once. Called 0 times.
根本原因:
  • 代码路径未执行
  • 在调用Mock前提前返回
  • 异常阻止了执行
  • Mock目标错误
调查步骤:
  1. 阅读测试内容 - 测试的是什么?
  2. 阅读实现代码 - save()是否真的被调用?
  3. 检查是否有提前返回或异常
  4. 验证Mock是否打在了正确的位置

Pattern 2: Attribute Error on Mock

模式2:Mock属性错误

Error:
AttributeError: Mock object has no attribute 'success'
Root Cause:
  • Mock returns dict when ServiceResult expected
  • Incomplete mock configuration
  • Missing return_value or side_effect
  • Wrong mock type
Investigation:
  1. Check what test expects mock to return
  2. Check what code actually returns
  3. Verify mock configuration matches interface
  4. Look for type mismatches (dict vs object)
错误:
AttributeError: Mock object has no attribute 'success'
根本原因:
  • Mock返回字典,但期望返回ServiceResult
  • Mock配置不完整
  • 缺少return_value或side_effect
  • Mock类型错误
调查步骤:
  1. 检查测试期望Mock返回什么
  2. 检查代码实际返回什么
  3. 验证Mock配置与接口匹配
  4. 查找类型不匹配(字典 vs 对象)

Pattern 3: Assertion Mismatch

模式3:断言不匹配

Error:
AssertionError: assert 'foo' == 'bar'
Root Cause:
  • Business logic error
  • Test expectation wrong
  • Data transformation issue
  • Configuration problem
Investigation:
  1. Is expected value correct?
  2. Is actual value correct?
  3. Is transformation logic correct?
  4. Are inputs to function correct?
错误:
AssertionError: assert 'foo' == 'bar'
根本原因:
  • 业务逻辑错误
  • 测试预期值错误
  • 数据转换问题
  • 配置问题
调查步骤:
  1. 预期值是否正确?
  2. 实际值是否正确?
  3. 转换逻辑是否正确?
  4. 函数输入是否正确?

Pattern 4: Import/Fixture Errors

模式4:导入/Fixture错误

Error:
ImportError: cannot import name 'X' from 'module'
fixture 'db_session' not found
Root Cause:
  • Missing dependency
  • Fixture not in scope
  • Circular import
  • Module not installed
Investigation:
  1. Check imports at top of file
  2. Check fixture definition location
  3. Check conftest.py for fixtures
  4. Verify dependencies installed
错误:
ImportError: cannot import name 'X' from 'module'
fixture 'db_session' not found
根本原因:
  • 依赖缺失
  • Fixture不在作用域内
  • 循环导入
  • 模块未安装
调查步骤:
  1. 检查文件顶部的导入
  2. 检查Fixture定义位置
  3. 检查conftest.py中的Fixture
  4. 验证依赖已安装

Troubleshooting

故障排除

See Troubleshooting section below for common issues.
常见问题请见下方故障排除章节。

Task Completion Checklist

任务完成检查清单

Before declaring task complete:
  • All tests pass (specific failing test)
  • Full test suite passes (no regressions)
  • Root cause identified and documented
  • All related errors fixed (not just first occurrence)
  • Quality gates pass
  • No new errors introduced
  • Fix addresses root cause, not symptoms
在标记任务完成前,请确认:
  • 所有测试通过(特定失败测试)
  • 全量测试套件通过(无回归)
  • 根本原因已识别并记录
  • 所有相关错误已修复(不仅是第一个出现的问题)
  • 质量门禁通过
  • 未引入新错误
  • 修复针对根本原因,而非症状

Framework-Specific Quick Reference

框架特定快速参考

Python (pytest)

Python (pytest)

Run tests:
bash
uv run pytest tests/ -v                    # All tests
uv run pytest tests/test_file.py -vv      # Specific file
uv run pytest tests/test_file.py::test_fn # Specific test
uv run pytest -k "pattern" -v             # Pattern matching
uv run pytest --lf -v                     # Last failed
Common flags:
  • -v
    - Verbose
  • -vv
    - Extra verbose
  • -s
    - Show print statements
  • --tb=short
    - Shorter traceback
  • --tb=long
    - Full traceback
  • --pdb
    - Drop into debugger on failure
运行测试:
bash
uv run pytest tests/ -v                    # 所有测试
uv run pytest tests/test_file.py -vv      # 特定文件
uv run pytest tests/test_file.py::test_fn # 特定测试
uv run pytest -k "pattern" -v             # 模式匹配
uv run pytest --lf -v                     # 仅运行上次失败的测试
常用参数:
  • -v
    - 详细输出
  • -vv
    - 超详细输出
  • -s
    - 显示打印语句
  • --tb=short
    - 简短堆栈跟踪
  • --tb=long
    - 完整堆栈跟踪
  • --pdb
    - 失败时进入调试器

JavaScript/TypeScript (Jest/Vitest)

JavaScript/TypeScript (Jest/Vitest)

Run tests:
bash
npm test                                  # All tests
npm test -- <test_file>                   # Specific file
npm test -- --testNamePattern="pattern"   # Pattern matching
npm test -- --verbose                     # Verbose output
Jest flags:
  • --verbose
    - Detailed output
  • --no-coverage
    - Skip coverage
  • --detectOpenHandles
    - Find async issues
  • --forceExit
    - Force exit after tests
Vitest flags:
  • --reporter=verbose
    - Detailed output
  • --run
    - Run once (no watch)
  • --coverage
    - Generate coverage
运行测试:
bash
npm test                                  # 所有测试
npm test -- <test_file>                   # 特定文件
npm test -- --testNamePattern="pattern"   # 模式匹配
npm test -- --verbose                     # 详细输出
Jest参数:
  • --verbose
    - 详细输出
  • --no-coverage
    - 跳过覆盖率统计
  • --detectOpenHandles
    - 查找异步问题
  • --forceExit
    - 测试后强制退出
Vitest参数:
  • --reporter=verbose
    - 详细输出
  • --run
    - 运行一次(不监听)
  • --coverage
    - 生成覆盖率报告

Go

Go

Run tests:
bash
go test ./...                             # All packages
go test -v ./pkg/service                  # Specific package
go test -run TestFunctionName             # Specific test
go test -v -race ./...                    # Race detection
运行测试:
bash
go test ./...                             # 所有包
go test -v ./pkg/service                  # 特定包
go test -run TestFunctionName             # 特定测试
go test -v -race ./...                    # 竞态检测

Rust

Rust

Run tests:
bash
cargo test                                # All tests
cargo test test_function_name             # Specific test
cargo test -- --nocapture                 # Show output
cargo test -- --test-threads=1            # Sequential
运行测试:
bash
cargo test                                # 所有测试
cargo test test_function_name             # 特定测试
cargo test -- --nocapture                 # 显示输出
cargo test -- --test-threads=1            # 顺序执行

Examples

示例

See examples.md for detailed walkthroughs of:
  • Mock configuration debugging
  • Business logic assertion failures
  • Integration test issues
  • Fixture scope problems
  • Cross-framework patterns
详细演练请见examples.md,包括:
  • Mock配置调试
  • 业务逻辑断言失败
  • 集成测试问题
  • Fixture作用域问题
  • 跨框架模式

Shell Scripts

Shell脚本

  • Test Skill Script - Helper script for testing and running test debug workflows
  • 测试技能脚本 - 用于测试和运行测试调试工作流的辅助脚本

Requirements

要求

Tools needed:
  • Bash (for running tests)
  • Read (for examining test and source files)
  • Grep (for finding all occurrences)
  • Glob (for pattern matching)
  • Edit/MultiEdit (for fixing code)
Test Frameworks Supported:
  • Python: pytest, unittest
  • JavaScript/TypeScript: Jest, Vitest, Mocha
  • Go: go test
  • Rust: cargo test
  • Ruby: RSpec
Project-Specific:
  • Access to test suite (tests/ directory)
  • Access to source code under test
  • Ability to run quality gates (./scripts/check_all.sh or equivalent)
Knowledge:
  • Basic understanding of test framework syntax
  • Ability to read stack traces
  • Understanding of mock/fixture patterns
所需工具:
  • Bash(用于运行测试)
  • Read(用于查看测试和源文件)
  • Grep(用于查找所有出现的问题)
  • Glob(用于模式匹配)
  • Edit/MultiEdit(用于修复代码)
支持的测试框架:
  • Python: pytest、unittest
  • JavaScript/TypeScript: Jest、Vitest、Mocha
  • Go: go test
  • Rust: cargo test
  • Ruby: RSpec
项目特定要求:
  • 可访问测试套件(tests/目录)
  • 可访问被测试的源代码
  • 可运行质量门禁(./scripts/check_all.sh或等效脚本)
知识要求:
  • 基本了解测试框架语法
  • 能够读取堆栈跟踪
  • 了解Mock/Fixture模式

Related Documentation

相关文档

  • Project CLAUDE.md: Critical workflow rules and debugging approach
  • Quality Gates: ../quality-run-quality-gates/references/shared-quality-gates.md
  • Testing Strategy: Project-specific testing documentation
  • 项目CLAUDE.md: 关键工作流规则和调试方法
  • 质量门禁: ../quality-run-quality-gates/references/shared-quality-gates.md
  • 测试策略: 项目特定测试文档

Philosophy

理念

This skill enforces evidence-based debugging:
  1. See actual errors (not assumed errors)
  2. Understand root causes (not symptoms)
  3. Fix systematically (not randomly)
  4. Validate thoroughly (not superficially)
The goal is not speed. The goal is correctness.
Slow down. Read the errors. Understand the problem. Fix it properly.
本技能强制实施基于证据的调试:
  1. 查看实际错误(而非假设的错误)
  2. 理解根本原因(而非症状)
  3. 系统性修复(而非随机尝试)
  4. 全面验证(而非表面检查)
目标不是速度,而是正确性。
慢下来,读取错误信息,理解问题,正确修复。