
How to test AI-suggested code before using it
To test AI-suggested code, start with the expected behavior, run a small example and compare the result. A convincing chat response still needs to meet the actual situation you want to solve.
We will use a function that cleans spaces in names without writing files or accessing external services. The goal is to learn a process you can repeat for other suggestions.
1. Write the rule before requesting code
Define what working means: remove spaces at either end and reduce repeated spaces between words to one. In this example, capitalization and accents should stay unchanged.
Also write down what was not requested, such as sorting names or looking up a customer database. That helps you notice when a response expands the task unnecessarily.
2. Create independent expectations
Choose inputs whose results you can check without consulting the implementation. A normal name, one with extra spaces and an empty string already reveal different paths.
| Input | Expected result |
|---|---|
Ana Silva |
Ana Silva |
Ana Silva |
Ana Silva |
| empty string | empty string |
João |
João |
The table acts as a small contract. If the AI changes the rule to make a test pass, compare it against that contract again.
3. Request a limited solution
Use a request that states the language, input and restrictions. Do not include real data when fictional examples are enough to reproduce the problem.
Write a Python function named normalizar_nome.
It takes a string, trims spaces at either end and reduces
repeated spaces between words to one.
Preserve capitalization and accents. Empty input returns empty.
Do not read files or add dependencies.
Explain how to check the four examples in my table.
Read the suggestion before running it and identify the inputs, output and operations performed. If an unfamiliar dependency appears, understand its purpose before continuing.
4. Run it and check the result
After defining the function in a test environment, you can start with direct checks:
assert normalizar_nome(" Ana Silva ") == "Ana Silva"
assert normalizar_nome("") == ""
assert normalizar_nome("João") == "João"
The Python assert documentation explains this check. Here it is a local demonstration rather than a replacement for application input validation.
5. Investigate one failure at a time
If a result differs, keep the input, expected output and actual output. Send that trio to the AI and request a focused correction without rewriting parts that already meet the rule.
Decide separately how non-text values should behave. Accepting, converting or rejecting an input should be your choice rather than an accidental detail of the response.
6. Record what was checked
Note which examples you ran and why each exists. When the function becomes part of a project, you can organize repeatable checks with unittest.
Useful evidence is being able to reproduce the result, including after a correction. Keep the tested version alongside the expectations that informed your decision.
Frequently asked questions
Where should I start testing AI-suggested code?
Write the expected result for a simple input before running the suggestion. Then include an edge case and an invalid input.
If the code ran without errors, is it correct?
No. Running without an error shows that one path executed; compare its output with the expected behavior.
Can I ask the same AI to write tests?
Yes, but review the expectations yourself. A test that repeats the same mistake as the code can pass without proving the requirement.