jixiaxue 知识库
blog / simon-willison-blog · first-run-the-tests

「先运行测试」— Simon Willison 智能体工程模式

2 个章节 · 0 条产出 · 0 条证据
2026-04-16

「先运行测试」— Simon Willison 智能体工程模式

原文:First run the tests 作者:Simon Willison | 系列:Agentic Engineering Patterns


核心观点

在 AI 编码智能体时代,自动化测试已从”最佳实践”升级为”必需品”。一条仅四个单词的提示——“First run the tests”——就能显著提升智能体的工作质量。


为什么测试不再是可选项

维度说明
旧借口失效过去”写测试太耗时、维护成本高”的理由已不成立——智能体几分钟就能搞定测试
AI 代码验证未经执行的 AI 生成代码能否正常工作纯粹靠运气,测试是唯一的验证手段
智能体上手利器智能体分析现有功能时,会主动查找和阅读相关测试代码
行为强化现有测试套件的存在会推动智能体为自己的改动也编写测试

四个单词提示的三重作用

每次启动新会话时,先输入:

First run the tests

这条简短提示同时实现三个目标:

  1. 建立测试意识 — 告诉智能体测试套件存在,迫使它学会如何运行测试,从而在后续开发中持续验证
  2. 传递项目信息 — 测试数量间接反映项目规模和复杂度,引导智能体通过测试代码深入了解项目
  3. 塑造测试思维 — 让智能体进入”测试优先”模式,自然地在后续工作中补充新测试

关键洞察

  • “First run the tests” 与 “Use red/green TDD” 同属四词提示策略——用极简指令激活模型中已内化的软件工程纪律
  • 智能体天生倾向于写测试,现有测试套件会放大这一倾向
  • 测试不仅是质量保障工具,更是智能体理解代码库的入口

实践建议

场景推荐提示
通用项目First run the tests
Python 项目(配置好 pyproject.toml)Run "uv run pytest"
开始任何智能体会话以运行测试作为第一步

先运行测试

先运行测试

原文:First run the tests 作者:Simon Willison 系列:Agentic Engineering Patterns(智能体工程模式)


在使用编码智能体(coding agents)进行开发时,自动化测试已经不再是可选项。

过去不写测试的那些老借口——测试耗时、在代码库快速迭代时维护成本高昂——在智能体可以用短短几分钟就把测试搞定的今天,已经站不住脚了。

测试对于确保 AI 生成的代码真的能做到它声称的事情也至关重要。如果代码从未被执行过,那它部署到生产环境后能正常工作纯粹靠运气。

测试同样是帮助智能体快速了解现有代码库的绝佳工具。观察一下当你让 Claude Code 或类似工具分析某个现有功能时会发生什么——它们很大概率会找到并阅读相关的测试代码。

智能体本身已经倾向于写测试,但现有测试套件的存在几乎肯定会进一步推动智能体为自己所做的改动编写测试。

每次我在一个已有项目上启动新的智能体会话时,我都会先输入这样一个提示:

First run the tests

对于我的 Python 项目,我已经配置好了 pyproject.toml,所以可以直接用这个提示:

Run "uv run pytest"

这些四个单词的提示有多重作用:

  1. 它告诉智能体存在测试套件,并迫使它搞清楚如何运行测试。 这使得智能体几乎一定会在之后运行测试来确保自己没有破坏任何东西。

  2. 大多数测试框架会让智能体大致了解有多少测试用例。 这可以作为项目规模和复杂度的一个间接指标,同时也暗示智能体如果想了解更多,可以去搜索和阅读测试代码本身。

  3. 它让智能体进入一种”测试优先”的思维模式。 在运行过测试之后,智能体自然会在后续开发中补充自己的测试。

与 “使用红/绿 TDD” 类似,“先运行测试”也是一个四个单词的提示,它浓缩了大量的软件工程纪律——而这些纪律已经内化在模型之中。

infographic

Create a professional infographic following these specifications:

Image Specifications

  • Type: Infographic
  • Layout: bento-grid
  • Style: craft-handmade
  • Aspect Ratio: 16:9 (landscape)
  • Language: Chinese (zh)

Core Principles

  • Follow the layout structure precisely for information architecture
  • Apply style aesthetics consistently throughout
  • If content involves sensitive or copyrighted figures, create stylistically similar alternatives
  • Keep information concise, highlight keywords and core concepts
  • Use ample whitespace for visual clarity
  • Maintain clear visual hierarchy

Text Requirements

  • All text must match the specified style treatment
  • Main titles should be prominent and readable
  • Key concepts should be visually emphasized
  • Labels should be clear and appropriately sized
  • Use Chinese for all text content

Layout Guidelines

Modular grid layout with varied cell sizes, like a bento box.

Structure

  • Grid of rectangular cells
  • Mixed cell sizes (1x1, 2x1, 1x2, 2x2)
  • No strict symmetry required
  • Hero cell for main point
  • Supporting cells around it

Visual Elements

  • Clear cell boundaries
  • Varied cell backgrounds
  • Icons or illustrations per cell
  • Consistent padding/margins
  • Visual hierarchy through size

Text Placement

  • Main title at top
  • Cell titles within each cell
  • Brief content per cell
  • Minimal text, maximum visual
  • CTA or summary in prominent cell

Style Guidelines

Hand-drawn and paper craft aesthetic with warm, organic feel.

Color Palette

  • Primary: Warm pastels, soft saturated colors, craft paper tones
  • Background: Light cream (#FFF8F0), textured paper (#F5F0E6)
  • Accents: Bold highlights, construction paper colors

Visual Elements

  • Hand-drawn or cut-paper quality
  • Organic, slightly imperfect shapes
  • Layered depth with shadows (paper variant)
  • Simple cartoon elements and icons
  • Character illustrations (people, personalities in cartoon form)
  • Ample whitespace, clean composition
  • Keywords and core concepts highlighted
  • Strictly hand-drawn—no realistic or photographic elements

Style Enforcement

  • All imagery must maintain cartoon/illustrated aesthetic
  • Replace real photos or realistic figures with hand-drawn equivalents
  • Maintain consistent line weight and illustration style throughout

Typography

  • Hand-drawn or casual font style
  • Clear, readable labels
  • Keywords emphasized with larger/bolder text
  • Cut-out letter style for paper variant

Generate the infographic based on the content below:

主标题:「先运行测试」— 智能体工程的四词秘诀

Hero Cell (2x2) — 核心观点

  • 大字标题:“First run the tests”
  • 副标题:四个单词,激活 AI 的软件工程纪律
  • 手绘卡通:一个机器人/智能体角色站在终端前,屏幕显示绿色的测试通过 ✓
  • 作者标注:Simon Willison · Agentic Engineering Patterns

Cell 1 (2x1) — 测试不再可选的四个理由

四个并排小卡片,每张一个手绘图标:

  1. ⏰ 旧借口失效 — 智能体几分钟搞定测试
  2. 🎯 AI 代码验证 — 未执行=纯靠运气
  3. 📖 上手利器 — 智能体主动阅读测试代码
  4. 🔄 行为强化 — 现有测试推动写新测试

Cell 2 (1x2) — 四词提示的三重作用

纵向三层,每层一个手绘图标和关键词:

  • 🧠 建立测试意识 → 迫使智能体学会运行测试
  • 📊 传递项目信息 → 测试数量反映项目规模
  • 💡 塑造测试思维 → 进入”测试优先”模式

Cell 3 (1x1) — 实践提示

手绘终端窗口样式:

> First run the tests
> Run "uv run pytest"

标注:每次新会话的第一步

Cell 4 (1x1) — 关键洞察

手绘灯泡图标 + 关键文字:

  • 四词提示策略:极简指令 → 激活内化纪律
  • 测试 = 智能体理解代码库的入口

Cell 5 (1x1) — 姊妹模式

手绘连接箭头:

  • “First run the tests” ↔ “Use red/green TDD”
  • 标注:同属四词提示家族

Text labels (in Chinese):

  • 主标题:「先运行测试」— 智能体工程的四词秘诀
  • 副标题:Simon Willison · Agentic Engineering Patterns
  • 核心观点:自动化测试已从”最佳实践”升级为”必需品”
  • 四个理由:旧借口失效、AI 代码验证、上手利器、行为强化
  • 三重作用:建立测试意识、传递项目信息、塑造测试思维
  • 实践提示:First run the tests / Run “uv run pytest”
  • 关键洞察:极简指令激活内化的软件工程纪律
  • 姊妹模式:Use red/green TDD