跳到正文
DEV Community· くるあああ·· 56 分鐘前AI 評分60

Claude Code 如何先把錯誤報告轉成失敗測試,再驗證修復

Make Claude Code prove its fix: turn any bug report into a failing test first

AI 導讀

作者分享一段供 Claude Code 使用的提示詞,要求先把錯誤報告轉成因所述原因而失敗的測試,再以最小改動修復並執行相關測試套件。提示詞亦要求提供測試名稱、修復前的失敗輸出、修復後的通過結果及一句根因説明;購物車折扣重複套用的案例中,測試顯示實際總額 $60、預期 $80。修復後保留回歸測試,讓測試套件可持續檢查該故障是否重現。

正文

The most dangerous sentence in AI-assisted coding is "this should fix it."

The agent patches something, the code compiles, everyone moves on — and the bug comes back two weeks later wearing a different hat. The fix was never proven. It was just plausible.

Here is the prompt I now paste before any bug fix in Claude Code. It forces test-first discipline on the agent:

Turn this bug report into a failing test first: [BUG_REPORT]. The test must fail for the reported reason — show me the failure output. Then fix the code minimally until it passes, and run the surrounding suite ([TEST_COMMAND]). Report: test name, the failure before, the pass after, and the one-line root cause.

Why each line matters

"Failing test first" — the agent has to reproduce the bug in code before it is allowed to touch the fix. No reproduction, no patch.

"Must fail for the reported reason" — tests can fail for the wrong reason (bad setup, wrong import). This line makes the agent check that the red test is actually your bug.

"Show me the failure output" — proof, not a summary. You see the red with your own eyes.

"Fix minimally until it passes" — minimal is doing real work in that sentence. It stops the agent from "improving" three unrelated files on the way.

"Report: failure before, pass after, one-line root cause" — the fix arrives with its own evidence attached, and the root cause has to fit in one line. If it can't, the agent doesn't understand it yet.

What it looks like in practice

Bug report: "discount shows twice in the cart."

The new test fails with total $60 actual vs. $80 expected — the coupon is applied twice. A minimal fix in the recalculation path turns it green, and the surrounding suite passes. The fix arrives with proof: red, then green. And the regression test stays in the suite, so this bug can never silently return.

That last part is the real compounding effect: every bug fixed this way leaves a test behind. Your suite grows out of real failures, not imagined ones.

The pattern generalises

Reproduce first. Demand the failure output. Fix minimally. Require the proof. It works for debugging, for reviews, for refactors — anywhere an agent might otherwise hand you confidence instead of evidence.

This prompt is one of 12 in my Debugging & Review Mini-Pack (root-cause analysis, git bisect walkthroughs, flaky-test autopsies, security-first review, and more), each with fill-in variables and a worked example:

Content first, pitch second — but if the prompt above saves you one 3 A.M. debugging session, the packs will pay for themselves.

來源:DEV Community · dev.to