Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 1

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

WHY

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

WHAT
  • 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

1 · Read understand every line 2 · Run small known example 3 · Test edge cases empty, zero, null, big 4 · Check deps + security real imports, no secrets 5 · Keep or fix green tests = keep

Worked example: a hidden bug

We asked an AI for “a Python function for the average of a list”. It gave this:

buggy.py
def average(nums):
    return sum(nums) / len(nums)
python -c "from buggy import average; average([])"
# -> ZeroDivisionError: division by zero

It 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.py
def 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.py
from fixed import average

def test_normal():
    assert average([2, 4, 6]) == 4
def test_empty():
    assert average([]) == 0.0
pytest -q
# -> 2 passed

How to review, step by step

HOW
  1. Read it line by line. If a line is unclear, ask the AI “explain line 4 in simple words”.
  2. Run it on one small example where you already know the answer.
  3. Try edge cases: empty, zero, negative, null/None, very long input.
  4. Ask the AI for tests, then read the tests too. A test that checks nothing is useless.
  5. Check every import on the official package site (PyPI, npm, Maven Central).
  6. Scan for security: no hard-coded keys, use parameterised SQL, validate user input.
  7. Run your linter and full test suite before you commit.

Install and config: tools that check for you

  1. Python: pip install pytest ruff then run ruff check . and pytest -q.
  2. JavaScript/TypeScript: Node 20+ has a built-in test runner: node --test. Add ESLint with npm init @eslint/config@latest.
  3. Java: JUnit 5 with Maven (mvn test) or Gradle (gradle test).
  4. Secrets: turn on GitHub secret scanning and push protection in repo Settings → Code security.
  5. CI: run the same lint + test commands in GitHub Actions on every pull request, so AI code is checked every time.
Rule: passing tests are your proof. “It looks fine” is not proof.

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.

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.

Try this yourself: fix safe_average and check it with tests

Environment (tested): these exercises were verified with 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)
HOW
  1. Create a project folder and open a terminal inside it.
  2. Create a venv: on macOS/Linux run python3 -m venv .venv. On Windows run py -3 -m venv .venv (or python -m venv .venv if that is your Python 3 launcher).
  3. Activate the venv: macOS/Linux source .venv/bin/activate. Windows Command Prompt .venv\Scripts\activate.bat. Windows PowerShell .venv\Scripts\Activate.ps1.
  4. Install pytest in the venv: python -m pip install pytest (after activation, python should be the venv interpreter).
  5. Save the buggy starter block below into a new file named safe_average.py (do not use a different filename).
  6. Reproduce the bug from the same folder: python -c "from safe_average import safe_average; safe_average([])". Expect ZeroDivisionError before you fix it.
  7. Replace the contents of the same safe_average.py file with your fix (empty list returns 0.0, otherwise mean with /).
  8. Save the tests below as test_safe_average.py in the same folder.
  9. Run: python -m pytest -q test_safe_average.py and expect the exact output line 4 passed.

Tests — save as test_safe_average.py

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):

terminal
python -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.

safe_average.py
"""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

HOW
  1. Symptom: ZeroDivisionError on [] — Fix: in safe_average.py, return 0.0 when the list is empty, then re-run the same pytest command.
  2. Symptom: safe_average([1, 2]) returns 1 instead of 1.5 — Fix: use true division /, not integer division //, then re-run.
  3. Symptom: import works for a differently named file but pytest cannot find safe_average — Fix: the module file must be named exactly safe_average.py in the current folder.
Practice label: inputs and names on this block are fictional teaching samples, not real people or accounts. Passing the listed tests checks those examples only.

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

SMART AI USE

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.

Save these cards

INTERVIEW CARDAI Coding

How do you review AI code?

Read, run, test edge cases, check imports, scan security, run CI.
Keep only what passes tests.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

Looks right vs is right

Looks right: clean, works on one example.
Is right: passes edge-case tests, real imports, no secrets.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

Review checklist

Read → Run → Edge cases → Imports → Security → Tests.
Green tests = proof.
Ravindra Bagale · ravindrabagale.com/ai-coding
Next in this course: see all chapters on the AI Coding page.