# Diagnose code errors and write useful tests with AI

> Build a reproducible failure, request a focused fix, and verify intended behavior and edge cases before merging.

**Author:** Awamer  
**Published:** 2026-11-11  
**Category:** Practical guides

---

The last line of an error message rarely explains the entire failure. An AI assistant needs intended behavior, observed behavior, and the environment in which they diverge. Aim for a testable explanation and a focused correction. Rebuilding a module is usually an unsuitable first response to a defect introduced by one small change.

## Build a reproducible diagnostic package

Include the language and framework versions, relevant dependencies, reproduction steps, a minimal code sample, and the complete sanitized error. Explain whether the failure is consistent or depends on particular inputs. For example, a fictional shopping-cart function may work for one item but miscalculate a discount across several items.

List previous attempts and their results. “It did not work” provides little evidence. Explain whether a change removed an exception but altered the calculation. Replace customer records and credentials with a small synthetic example that preserves the failure conditions.

Environment: [language, version, framework]. Expected behavior: [description]. Actual behavior: [description]. Reproduction steps: [steps]. Sanitized code and error: [sample]. Previous attempts: [results]. Rank plausible causes and propose a check that distinguishes them. Once supported, suggest the smallest fix and explain its impact. Identify missing information rather than inventing files or functions.

## Derive tests from requirements

Write the valid, invalid, and boundary cases before generating test code. Consider empty input, negative quantities, rounding, large values, and unavailable external services where relevant. Calculate one small example manually. Expected values must come from intended behavior rather than simply copying the current implementation.

Use the [function test planner](/en/ai-prompts/function-test-planner) to separate the case list from executable tests. Keep fixtures understandable and isolate external dependencies when they are outside the behavior under test. Demonstrate the original failure, apply the change, and run the affected checks again.

## Review the change and its evidence

Inspect the diff for changes to permissions, data formats, error handling, and dependencies. Run appropriate formatting and automated checks. For a concurrency or network defect, test the conditions that caused the failure; an unrelated unit test cannot establish that the issue is resolved.

Record the cause, reproducing case, fix, and validation in the change description. The [error diagnosis template](/en/ai-prompts/debug-error-context) supports the next investigation, while the [database design template](/en/ai-prompts/database-schema-designer) helps when the underlying data model is the problem. Accept code on the basis of observable behavior, not the confidence of its explanation.
