Failing Test First Bug Fix

Reproduces a bug with a failing test, finds the root cause, applies the smallest fix and shows the test passing.

You get: Working code with an explanation

More like this

The prompt

failing-test-first-bug-fix.md
You are a senior engineer who fixes bugs test-first. Here is the bug report or error: {paste your bug report or error here}. If you do not know the language, test framework, or the relevant code, ask me up to two short questions first. Then work in this order. 1) Reproduce: write the smallest failing test that captures the bug and state expected versus actual behavior. 2) Root cause: name the exact line or condition responsible and why, keeping confirmed facts separate from assumptions. 3) Fix: apply the minimal change with no refactoring or unrelated cleanup. 4) Prove: show the test passing and name existing tests that could be affected. Rules: never claim a test passed unless you actually ran it; if you cannot run code, show the expected output and say so. Keep the diff as small as possible. Output format: Assumptions, Failing test, Root cause, Minimal fix (as a diff), Test result, Risks and missing information, Next actions. The answer is good if the test fails before the fix, passes after it, and I can decide whether to ship.

Example output

AI-written example
# ===== Assumptions (not confirmed) =====
# - Python 3 with pytest; the function lives in pagination.py as quoted in the report.
# - Bug report: "25 items with page size 10 shows 2 pages; the last 5 items are unreachable."
# - Pages are 1-indexed. Zero items means zero pages.

# ===== Failing test (tests/test_pagination.py) =====
import pytest
from pagination import page_count

@pytest.mark.parametrize("total, size, expected", [
    (25, 10, 3),   # reported bug: partial last page is dropped
    (1, 10, 1),    # fewer items than one page
    (20, 10, 2),   # exact multiple must stay unchanged
    (0, 10, 0),    # empty list
])
def test_page_count_includes_partial_last_page(total, size, expected):
    assert page_count(total, size) == expected

# Expected vs actual BEFORE the fix:
#   page_count(25, 10) -> expected 3, actual 2
#   page_count(1, 10)  -> expected 1, actual 0
# Not executed here. Expected pytest output before the fix:
#   FAILED test_pagination.py::test_page_count_includes_partial_last_page[25-10-3]
#   FAILED test_pagination.py::test_page_count_includes_partial_last_page[1-10-1]
#   2 failed, 2 passed

# ===== Root cause =====
# Confirmed (from the code in the report):
#   pagination.py line 2 uses floor division: `total_items // page_size`.
#   Floor division discards the remainder, so a partial last page is never counted.
# Assumption (not verified):
#   The API's "has_next" logic relies on page_count, so the missing page also
#   breaks next-page links. I have not seen that code.

# ===== Minimal fix (diff) =====
# --- a/pagination.py
# +++ b/pagination.py
# @@ -1,2 +1,2 @@
#  def page_count(total_items: int, page_size: int) -> int:
# -    return total_items // page_size
# +    return -(-total_items // page_size)  # ceiling division, no new import

# ===== Test result =====
# NOT RUN: I cannot execute code in this session. Expected output after the fix:
#   test_pagination.py ....                                  [100%]
#   4 passed
# Run `pytest tests/test_pagination.py -v` to confirm the test fails
# before the patch and passes after it.

# ===== Risks and missing information =====
# - Existing tests that could be affected: test_get_page_last_page,
#   test_page_count_exact_multiple,
Generated from this prompt by AI. Your results will vary.

Tips and common edits

Use when you have a reproducible bug and want a regression test along with the fix. Paste the bug report or error, and share the relevant code or let an agent read your repo.

Questions about this prompt

More in Coding

Characterization Tests Before a Refactor

Writes tests that lock in a module's current behavior, including edge cases and quirks, so you can refactor safely.

Codebase Tour for a Specific Task

Explores a repo through the lens of your task and explains the architecture, key files and data flow, then says where to start.

Explain This Error with Ranked Causes

Explains an error and stack trace in plain words, ranks the three most likely causes and gives a confirming check for each.

Root Cause Analysis from Stack Trace and Logs

Turns an error report, stack trace, and logs into ranked root-cause hypotheses. Includes the cheapest checks to confirm each one, a fix proposal, and a regression test plan.

Pull Request Review with Merge Recommendation

Reviews a diff against your standards and returns a merge, merge-with-changes, or do-not-merge recommendation. Findings are ranked by severity, with evidence, suggested fixes, and open questions.

Unit Test Plan and Test Code for an Existing Function

Builds a behavior-based test matrix for a function or module and then generates test code in your framework. Covers edge cases, failures, and mocking boundaries, with coverage gaps stated honestly.

Architecture Decision Record for a Technology Choice

Compares realistic options against your constraints and produces a decision record. The record has a weighted comparison, risks, a reversal plan, and a recommendation the team can approve or challenge.

Debug an Error From the Message and the Code Around It

Finds the most likely cause of an error from the message and the relevant code, ranks other possible causes, and gives the smallest fix plus a way to prove it worked.

Regex with Match and Non-Match Test Strings

Writes a regex for your pattern, explains each part and provides 8 strings that should match and 8 that should not.

SQL Query from Schema and Question

Writes a SQL query that answers your question from your schema, explains each clause and suggests indexes worth adding.

Cursor .mdc Rules to Portable AGENTS.md Converter

Converts your pasted Cursor .mdc rules into a single AGENTS.md that Codex, Copilot and Gemini CLI can read, plus a table of what does not carry over.

Test-First Bug Fix Skill: Reproduce, Fix, Prove (SKILL.md)

A SKILL.md that forces your coding agent to reproduce a bug with a failing test before changing code, apply the smallest fix, and prove it with the test suite.