Test-First Bug Fixing Approach
Contributed by ilkerulusoy
Improved by Laravel Company · 2026-09-07
Improved prompt:
You are a seasoned software engineer tasked with fixing a critical bug in a large codebase. Here is the bug report:
${bug_report}
To approach this debugging task, follow these steps rigorously:
Codebase Familiarization
- Carefully review the relevant source files: ${relevant_files}.
- Study the existing unit tests in ${test_directory}.
- Identify any related functionality or dependencies.
Test-Driven Debugging
- Write a new unit test that precisely reproduces the given bug.
- The test should:
- Use the same input parameters as the bug report.
- Have a clear and descriptive name (e.g.,
test_user_authentication_failure). - Be placed in a location that makes sense (e.g., the
integrationorfunctionaldirectory).
- Verify the test fails by running the entire test suite (
pytest -v).
Minimal Fix Implementation
- With the failing test confirmed, implement the smallest possible change to make the test pass.
- Your fix should:
- Solve the reported issue.
- Not introduce new bugs.
- Be commented clearly (e.g.,
// Fix for issue #1234: User authorization failure).
Regression Test
- Run the full test suite again (
pytest -v). - Analyze any new failures that arise.
- Run the full test suite again (
Iterative Refactoring
- If any related tests fail, analyze the failure logs.
- Adjust your fix as needed.
- Re-run the full test suite until all tests pass.
Codebase-wide Analysis
- Use a command-line tool (e.g.,
grepor a code search platform) to find other parts of the codebase that might be affected by the same issue. - Write tests for these potential problem areas.
- Add them to the test suite.
- Use a command-line tool (e.g.,
Change Documentation
- Document every change made in a commit message.
- Summarize the bug, the fix, and the reasoning behind each change.
- Include a reference to the related issue (e.g.,
#1234).
Autonomy and Assumptions
- Make reasonable assumptions to proceed without needing to ask clarifying questions.
- Document these assumptions in the commit message or the bug report summary.
- If a key piece of information is missing, assume the standard or most common behavior for your field.
Do not ask me for clarificationâproceed with the assumptions stated above and document your work diligently.
Original prompt (before our improvements)
I have a bug: ${bug}. Take a test-first approach: 1) Read the relevant source files and existing tests. 2) Write a failing test that reproduces the exact bug. 3) Run the test suite to confirm it fails. 4) Implement the minimal fix. 5) Re-run the full test suite. 6) If any test fails, analyze the failure, adjust the code, and re-run—repeat until ALL tests pass. 7) Then grep the codebase for related code paths that might have the same issue and add tests for those too. 8) Summarize every change made and why. Do not ask me questions—make reasonable assumptions and document them.