# Test And Fix

> 自动测试修复技能 - 运行测试、解析失败、AI自动修复循环

- Skill: `chainlesschain/test-and-fix` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add chainlesschain/test-and-fix`
- Raw SKILL.md: https://api.skillmd.com/api/skills/chainlesschain/test-and-fix/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Web & Frontend
- Author: chainlesschain (https://skillmd.com/u/chainlesschain)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/chainlesschain/test-and-fix

---


# 自动测试修复技能

## 描述

自动检测测试框架，运行测试套件，解析失败信息（包括堆栈跟踪），AI 分析失败原因并修复代码，重复直到所有测试通过。

## 使用方法

```
/test-and-fix [测试路径] [选项]
```

## 选项

- `--source-only` - 仅修复源代码，不修改测试文件
- `--test-only` - 仅修复测试代码（更新断言等）
- `--affected-by <file>` - 只运行受指定文件影响的测试
- `--max-iterations <n>` - 最大修复迭代次数 (默认: 5)
- `--runner <name>` - 指定测试运行器: vitest, jest, pytest, junit

## 支持的测试框架

| 框架   | 语言        | 配置检测                          |
| ------ | ----------- | --------------------------------- |
| Vitest | JS/TS       | vitest.config._, vite.config._    |
| Jest   | JS/TS       | jest.config.\*, package.json jest |
| Pytest | Python      | pytest.ini, pyproject.toml        |
| JUnit  | Java/Kotlin | build.gradle, pom.xml             |

## 执行流程

```
1. 检测测试框架和配置
        ↓
2. 运行测试套件
        ↓
3. 解析失败 (堆栈跟踪、断言错误、超时)
        ↓
4. AI 诊断失败原因:
   - 源代码 bug → 修复源码
   - 测试断言过时 → 更新测试
   - 环境问题 → 报告
        ↓
5. 应用修复
        ↓
6. 重新运行失败测试 ←─── 还有失败? (最多5次)
        ↓
7. 运行全套测试确认无回归
        ↓
8. 输出报告
```

## 输出格式

```
Test & Fix Report
=================
Runner: Vitest 3.x
Config: vitest.config.ts

Initial run: 157 tests
  ✅ 152 passed
  ❌ 5 failed

Round 1: Fixing 5 failures
  - src/main/llm/session-manager.js:45 → fixed null check
  - src/main/rag/bm25-search.js:120 → fixed index bounds
  - tests/unit/hooks.test.js:89 → updated expected value
  - 2 remaining

Round 2: Fixing 2 failures
  - src/main/utils/ipc-error-handler.js:67 → fixed async handling
  - Fixed

Final run: 157/157 passed ✅
Files modified: 3 source, 1 test
```

## 示例

修复所有失败测试:

```
/test-and-fix tests/unit/
```

只修复受影响的测试:

```
/test-and-fix --affected-by src/main/llm/context-engineering.js
```

