Community写作与编辑github.com

MoonshotAI/test

在编写或审查测试时使用,或当被询问如何写好单个测试时使用。编码了“测试契约/职责,而非实现”原则下的每条测试规则——每个`it`命名和结构一个行为,通过公共接口驱动,仅桩化真正的外部边界,通过文档化的旋钮控制时间和配置,并保持测试清晰、隔离且重构友好。同样的规则驱动编写(写模式)和审查现有测试(审查模式)。

test 是什么?

该技能基于“测试契约/职责,而非实现”的核心原则,为编写和审查单个测试提供了一套可操作的规则。它支持两种模式:**写模式**用于创建新测试,**审查模式**用于审计现有测试,两者共享相同的规则集,只是姿态不同(生产 vs. 审计)。技能详细定义了每个`it`应覆盖单一行为、通过公共接口驱动系统并断言可观察效果、仅桩化真正的外部边界(如远程服务、网络、时间)、使用字面量期望值以保持DAMP(描述性且有意义的短语)、确保测试独立无污染,并最终通过CCCR(清晰性、完整性、简洁性、韧性)质量标准进行校验。该技能适用于任何支持AI Agent的开发环境,如Claude Code、Cursor或Codex,通过提供结构化的规则和审查清单,帮助开发者写出高质量、可维护、重构友好的测试,并能在代码审查中快速识别和报告违规点。

兼容平台~Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/MoonshotAI/kimi-code/tree/main/.agents/skills/agent-core-review/test

Installed? Explore more 写作与编辑 skills: steipete/notion, langchain-ai/langchain, bytedance/podcast-generation · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

测试 — 编写与审查

每项测试规则将一条原则付诸实践:测试契约/职责,而非实现。这是单个 it 的“如何做”,也是审查单个 it 的视角。

两种模式,一套规则

  • 写模式 — 编写测试。应用以下规则来生成测试。
  • 审查模式 — 审计现有测试或测试差异。将相同的规则作为检查清单应用;每条违规报告格式:file:line、违反的规则以及修复建议。参见结尾的“审查模式”。

两种模式下的规则完全相同——只是姿态变了(生成 vs. 审计)。

测试契约,而非实现

  • 通过系统的公共控制面驱动,并断言可观察的效果(返回值、持久化状态、发送的事件、注入的消息),绝不针对源码细节。
  • 通过协作对象的契约——接口及其标识符——来解析协作对象,而不是绑定具体实现的模块。
  • 不要深入私有字段或为“测试”添加后门。如果你觉得有必要,说明接缝是错误的——修复设计,而不是测试。

每个 it 一个行为

每个 it 覆盖恰好一个职责/场景。如果名称中需要“和”,请拆分。

it('当调用者未授权时返回 401', ...);
it('当同一 tick 重复时不会重复触发', ...);

命名与结构

  • describe('<切片> (<职责>)' — 命名职责,而不是类。
  • it(...) 读起来应像一个句子,但它仍然必须编码三样东西——行为/方法、状态或条件以及预期结果:it('<行为> 当 <条件> 时, <结果>')。像 当 Y 时做 X 这样的名称没有结果,无法有效失败。
    • 使用空格,而不是 Java 风格的 method_state_outcome 下划线——这种约定只是因为 Java 测试方法名不能包含空格。字符串命名的测试读起来就像句子一样自然。
    • 好的:it('当调用者未授权时返回 401') · it('推进光标并在重复 tick 上不重复触发')
    • 坏的:it('工作正常') · it('正确处理认证') — 没有条件,没有结果
  • 安排/行动/断言。当有助于阅读时,简短的 // Given // When // Then 是可以的;不要在琐碎的测试上机械地粘贴它们。

构建一个小型测试工具

当多个测试共享设置时,编写一个工厂函数(rig()、createHost() 或任何适合代码库的名称),返回测试所需的最小表面。测试访问工具;它们不需要每次都重新构建整个世界。保持工具简单:只负责连线,不包含断言。

仅桩化真正的外部边界

默认使用生产环境连线方式的真实协作对象。桩化最小接缝,该接缝是真正外部的:

  • 远程/模型/服务边界 — 监视契约方法(接口),捕获系统通过它发送的内容。不要启动真实的外部东西。
  • 网络/其他进程边界 — 在边界处桩化,而不是内部实现。
  • 时间、定时器、抖动 — 使用系统暴露的文档化控制旋钮(环境变量、注入的时钟、手动 tick)。不要使用假定时器或真实的 setTimeout 来驱动时间。
  • 环境/配置旋钮通常在引导时快照 — 在构建被测系统之前设置它们,并在 afterEach 中恢复。

保持测试 DAMP 并让原因紧邻结果

  • DAMP 优于 DRY:在断言中使用字面预期值;不要使用与测试代码相同的逻辑来计算期望值。
  • 将关键前置条件放在 it(或其工具)内部,这样读者可以看到原因紧邻结果。将 beforeEach 保留给横切性的管道工作(环境快照、清理),而不是用于隐藏场景的设置。
// 好的 — 期望值是读者可以检查的字面量。
expect(discount).toBe(15);
// 坏的 — 重新推导期望值;镜像了实现。
expect(discount).toBe(price * rate);

仅断言相关的内容

断言证明契约的效果。使用匹配器/部分对象匹配忽略附带字段。不要断言内部计数器、调用顺序或用户不能依赖的形状。

隔离与清理(无随机失败)

每个测试必须是密封的且顺序无关。在 afterEach 中:

  • 恢复每个 mock / spy
  • 恢复每个你碰过的环境变量(在 beforeEach 中快照)
  • 释放主机/容器并重置其引用

不依赖挂钟时间、运行顺序或残留的磁盘状态——当状态持久化时,为每个场景提供独立的身份/工作空间。

质量标准:CCCR

在完成之前,根据以下内容检查每个测试:

  • 清晰性 — 陌生人仅从失败消息就能判断出哪里出了问题。
  • 完整性 — 覆盖职责的成功、错误和边界路径。
  • 简洁性 — 没有重复或推测性的案例;每个 it 一个场景。
  • 韧性 — 内部重构后测试无需更改(因为断言的是契约,而非实现)。

每个文件的场景头部

在每个测试文件的开头添加一个简短的头部注释:场景、被断言的职责、连线(哪些协作者是真实的 vs. 唯一被桩化的边界)以及如何运行它。

审查模式 — 审计现有测试

将上述规则作为检查清单应用于作用域内的每个测试(文件、差异或命名的 it)。对于每条命中,报告 file:line + 它违反的规则 + 修复建议;除非被要求,否则不要重写。以契约问题为引导:这个测试证明了什么可观察的行为,它能在重构后存活吗?

按顺序检查:

  1. 契约,而非实现 — 断言可观察的效果,而不是私有字段、调用顺序或用户不能依赖的内部形状。
  2. 每个 it 一个行为 — 名称包含行为 + 条件 + 结果;名称中的“和”意味着需要拆分。
  3. 边界纪律 — 只有真正的外部接缝被桩化;时间由文档化的旋钮驱动,而不是假定时器/真实的 setTimeout。
  4. DAMP 期望 — 期望值是字面量,而不是由测试代码的逻辑重新推导的。
  5. 隔离 — mock/spy/环境变量/主机在 afterEach 中恢复;不依赖挂钟时间、运行顺序或残留的磁盘状态。
  6. CCCR 通读 — 清晰性、完整性(成功/错误/边界)、简洁性、韧性。

报告结果以证据+修复的形式,例如:“foo.test.ts:42 断言了 service.internalMap(契约)——改为断言返回值。”如果一个测试通过了检查,简要说明;对某条规则的沉默意味着它通过了。

快速检查清单(编写与审查)

  • 通过契约解析;没有具体实现的导入
  • 每个 it 一个行为;名称包含行为 + 条件 + 结果;AAA
  • 仅桩化了真正的外部接缝;时间通过旋钮驱动,而不是假定时器
  • 字面期望值;仅相关的断言
  • 在 afterEach 中恢复 mock/环境变量/主机;密封,无随机失败
  • CCCR 通读已完成

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

MoonshotAI/agent-core-dev

Use when developing in packages/agent-core-v2 (the DI × Scope agent engine) — adding or modifying a domain Service, choosing a LifecycleScope, wiring DI dependencies, splitting a domain across scopes, owning or migrating a config section, gating behavior behind an experimental flag, raising coded errors, working on the permission system, writing DI/Scope tests, porting business logic from agent-core (v1) to v2, triaging a main-branch commit against v2, or exposing a v2 domain over server-v2 while keeping the /api/v1 wire contract compatible with released clients. Self-contained guide organized by development stage (orient → design → implement → test → verify) plus align workflows for v1→v2 migration, main-branch commit triage, and server-v2 wire exposure; each file carries the rules, examples, and red lines for its step.

MoonshotAI/agent-core-review

Use ONLY for code review and test write/review guidance in `packages/agent-core-v2` (the DI × Scope agent engine). Does NOT apply to the legacy `packages/agent-core` or to any other package — for those, do not load this skill. Groups the review and testing lenses used for agent-core-v2 — `slop` (single-level-of-abstraction / layered error-handling review, invoked only on explicit request) and `test` (contract-driven per-test rules for both authoring and reviewing tests). Apply the sub-skill that matches the task; do not apply `slop` unprompted.

MoonshotAI/consolidate

Apply an approved sub-skill grouping by moving user-specified skills into a parent bundle, with timestamped backups of every modified directory.

MoonshotAI/gen-changesets

Use when generating changesets in the kimi-code repository, including package bump selection, internal package and CLI bundle handling, bump levels, major confirmation, and English changelog wording.

MoonshotAI/gen-docs

Update Kimi Code CLI user documentation after meaningful code changes that affect product behavior or user experience.

MoonshotAI/kimi-datasource

Universal data-source assistant. Use this skill when the user wants external structured data such as stocks, financial reports, technical indicators, A-share/HK/US markets, global macroeconomics, Chinese enterprise registry information, arXiv papers, Google Scholar results, or Chinese laws/regulations and judicial cases. This plugin exposes tools via MCP server `plugin-kimi-datasource_data`; call them in the flow `mcp__plugin-kimi-datasource_data__get_data_source_desc` → `mcp__plugin-kimi-datasource_data__call_data_source_tool`.

MoonshotAI/pre-changelog

Use before merging a kimi-code release PR to preview the user-facing CLI changelog in Chinese. Reads the changelog that changesets pre-generated in the release PR, then reuses sync-changelog's strip / classify / translate logic to render a Chinese preview. Writes no files.

MoonshotAI/review

Analyze the available skill set and recommend candidate groups that could be consolidated into sub-skill bundles. Read-only — proposes a plan, does not move files.

MoonshotAI/slop

Invoke only when the user explicitly asks to review code through the "single level of abstraction / layered error handling" lens — a function does only its own layer's business logic while errors are handled above or below. The agent reports detections, raw-count measurements, and move directions. Apply only when the user explicitly requests this lens.

MoonshotAI/sub-skill

Discover and reorganize the skill inventory into hierarchical sub-skill bundles. Use when the user asks to review, group, or consolidate skills into a parent bundle.

MoonshotAI/sync-changelog

Use after a release succeeds, when maintainers need to sync apps/kimi-code/CHANGELOG.md into docs/en/release-notes/changelog.md and docs/zh/release-notes/changelog.md, then open a PR on a dedicated branch.

MoonshotAI/translate-docs

Translate and sync bilingual user documentation between docs/zh/ and docs/en/ following the source-of-truth rules in docs/AGENTS.md.

MoonshotAI/write-tui

Use when writing or modifying the kimi-code terminal UI in apps/kimi-code/src/tui — components, dialogs/selectors, slash commands, themes, streaming render, or the KimiTUI controllers. Covers the architecture, where new features go, test placement, the theme system mechanics, and the dialog interaction/visual spec (DESIGN.md).

相关技能