How to Review and Test AI-Written Code
AI code often looks right. Sometimes it is wrong. Here is a simple checklist to read it, run it and test it before you keep it.
In short: read every line, run it on a small example, test the edge cases, check imports are real, and scan for secrets and security issues.
Why you must review
An AI predicts likely code. It does not run it for you unless you ask an agent to. It can miss edge cases, use an old API or invent a library. You own the code you ship, not the AI.
What to look for
- Logic errors: right shape, wrong answer (off-by-one, wrong condition).
- Missing edge cases: empty list, zero, null, very large input.
- Made-up APIs: a function or package that does not exist. Always check imports.
- Old APIs: a method that was renamed or removed.
- Security issues: SQL built by joining strings, secrets in code, no input checks.
- Licence risk: a big block that looks copied from a known project.
How a review flows
Worked example: a hidden bug
We asked an AI for “a Python function for the average of a list”. It gave this:
buggy.pydef average(nums):
return sum(nums) / len(nums)python -c "from buggy import average; average([])"
# -> ZeroDivisionError: division by zeroIt works for normal lists. It crashes on an empty list. We asked: “Handle an empty list by returning 0.0, add type hints and a test.”
fixed.pydef average(nums: list[float]) -> float:
"""Average of a list. Empty list returns 0.0 instead of crashing."""
if not nums:
return 0.0
return sum(nums) / len(nums)test_fixed.pyfrom fixed import average
def test_normal():
assert average([2, 4, 6]) == 4
def test_empty():
assert average([]) == 0.0pytest -q
# -> 2 passedHow to review, step by step
- Read it line by line. If a line is unclear, ask the AI “explain line 4 in simple words”.
- Run it on one small example where you already know the answer.
- Try edge cases: empty, zero, negative, null/None, very long input.
- Ask the AI for tests, then read the tests too. A test that checks nothing is useless.
- Check every import on the official package site (PyPI, npm, Maven Central).
- Scan for security: no hard-coded keys, use parameterised SQL, validate user input.
- Run your linter and full test suite before you commit.
Install and config: tools that check for you
- Python:
pip install pytest ruffthen runruff check .andpytest -q. - JavaScript/TypeScript: Node 20+ has a built-in test runner:
node --test. Add ESLint withnpm init @eslint/config@latest. - Java: JUnit 5 with Maven (
mvn test) or Gradle (gradle test). - Secrets: turn on GitHub secret scanning and push protection in repo Settings → Code security.
- CI: run the same lint + test commands in GitHub Actions on every pull request, so AI code is checked every time.
Ravindra Bagale's Tip
When you review AI code, first run the empty or zero case. Many AI answers look neat and still crash on empty input.
Ravindra Bagale's Tip – मराठी
AI code review करताना आधी empty किंवा zero case चालवा. बरेच AI उत्तरं नीट दिसतात, पण empty input वर crash होतात.
Ravindra Bagale's Tip – हिंदी
AI code review करते समय पहले empty या zero case चलाओ. कई AI जवाब साफ दिखते हैं, पर empty input पर crash हो जाते हैं.
Hello friends. We will save a small buggy function on purpose, reproduce the crash, replace the file with a fix, and run tests for this example. This is practice code only. Do not use real bank numbers or live API keys. These tests check the cases below. They do not prove every possible input is safe.
नमस्कार मित्रांनो. आपण मुद्दाम एक छोटं buggy function save करू, crash reproduce करू, नंतर file fix ने replace करू, आणि या example साठी tests चालवू. हे फक्त practice code आहे. खरे bank number किंवा live API key वापरू नका. हे tests खालील cases तपासतात. प्रत्येक possible input सुरक्षित आहे असे ते सिद्ध करत नाहीत.
नमस्कार दोस्तों. हम जानबूझकर एक छोटा buggy function save करेंगे, crash reproduce करेंगे, फिर file को fix से replace करेंगे, और इस example के tests चलाएंगे. यह सिर्फ practice code है. असली bank number या live API key मत इस्तेमाल करना. ये tests नीचे वाले cases जाँचते हैं. हर possible input सुरक्षित है, यह वे साबित नहीं करते.
Try this yourself: fix safe_average and check it with tests
Python 3.13.5 and pytest 9.2.0. Use Python 3.10+ (needed for list[float] style hints as written). Create a folder on your computer, open a terminal in that folder, then set up a venv and install pytest before you run tests.Fictional practice data only.
Your task: an AI wrote safe_average(nums) that crashes on an empty list. Save the buggy starter below as safe_average.py (exact filename). Reproduce the crash. Then replace the contents of the same safe_average.py file with a fix: return 0.0 for an empty list, otherwise return the arithmetic mean using true division (/, not //) as a float. Examples: safe_average([2, 4, 6]) returns 4.0, safe_average([]) returns 0.0, and safe_average([1, 2]) returns 1.5.
Also create test_safe_average.py from the tests block below.
Buggy starter — save this exact text as safe_average.py first
safe_average.py"""Intentional buggy starter for the review exercise (fictional practice)."""
def safe_average(nums: list[float]) -> float:
return sum(nums) / len(nums)
- Create a project folder and open a terminal inside it.
- Create a venv: on macOS/Linux run
python3 -m venv .venv. On Windows runpy -3 -m venv .venv(orpython -m venv .venvif that is your Python 3 launcher). - Activate the venv: macOS/Linux
source .venv/bin/activate. Windows Command Prompt.venv\Scripts\activate.bat. Windows PowerShell.venv\Scripts\Activate.ps1. - Install pytest in the venv:
python -m pip install pytest(after activation,pythonshould be the venv interpreter). - Save the buggy starter block below into a new file named
safe_average.py(do not use a different filename). - Reproduce the bug from the same folder:
python -c "from safe_average import safe_average; safe_average([])". ExpectZeroDivisionErrorbefore you fix it. - Replace the contents of the same
safe_average.pyfile with your fix (empty list returns0.0, otherwise mean with/). - Save the tests below as
test_safe_average.pyin the same folder. - Run:
python -m pytest -q test_safe_average.pyand expect the exact output line4 passed.
Tests — save as test_safe_average.py
"""Tests for fictional safe_average practice."""
from safe_average import safe_average
def test_normal():
assert safe_average([2, 4, 6]) == 4.0
def test_empty():
assert safe_average([]) == 0.0
def test_single():
assert safe_average([9.0]) == 9.0
def test_half():
assert safe_average([1, 2]) == 1.5
Command and expected output (venv active):
terminalpython -m pytest -q test_safe_average.py
# -> 4 passed
Optional solution for safe_average.py (try yourself first)
Use this only after you attempt the task. Save or compare against your safe_average.py file.
"""Fictional practice: average that tolerates an empty list."""
def safe_average(nums: list[float]) -> float:
"""Return the mean of nums. An empty list returns 0.0."""
if not nums:
return 0.0
return sum(nums) / len(nums)
Troubleshooting this exercise
- Symptom:
ZeroDivisionErroron[]— Fix: insafe_average.py, return0.0when the list is empty, then re-run the same pytest command. - Symptom:
safe_average([1, 2])returns1instead of1.5— Fix: use true division/, not integer division//, then re-run. - Symptom: import works for a differently named file but pytest cannot find
safe_average— Fix: the module file must be named exactlysafe_average.pyin the current folder.
Quick quiz
1. What bug did the AI average() have?
It crashed with ZeroDivisionError on an empty list.
2. How do you check an import is real?
Look it up on the official package site, like PyPI, npm or Maven Central.
3. Name three edge cases to test.
Empty input, zero/negative, null/None (also very large input).
4. Why read the AI's tests too?
A test can pass while checking nothing useful.
Save tokens and money
Do not paste the whole project for review. Select only the changed function and ask “find bugs and missing edge cases”. Run linters and tests locally first, they are free, then ask the AI only about real failures.