Catch the bug that looks right

Module 1: Code with AI, and check every line

Course outline

Spot the mistakes

Guess first, no penalty. An AI wrote average_order_value(). Customer A has orders of 10 and 20, customer B one order of 100. A's result comes out as 65.0, not 15.0. Tap the line that causes it.

The function, line by line

0 of 1 found

The worst AI bugs don't crash. The code runs and returns tidy numbers, and they're wrong. A quick read misses them because each line looks fine on its own.

Two habits catch them:

  • Trace one small example by hand. Pick inputs you can work out in your head and compare with what the code returns.
  • Write a test with mixed input. One customer hides this bug, because the shared total is that customer's total. Two customers expose it.

The expected value in a test comes from your hand trace or the requirement. Never paste in what the code returned, or the test locks the bug in.

A worked example

Hand trace:  A = (10 + 20) / 2 = 15.0    B = 100 / 1 = 100.0
Code says:   A = 65.0                    B = 130.0      -> bug

Fix: keep one total per customer:
    totals[c] = totals.get(c, 0) + order["amount"]

Test I keep:
    orders = [{"customer": "A", "amount": 10.0},
              {"customer": "A", "amount": 20.0},
              {"customer": "B", "amount": 100.0}]
    assert average_order_value(orders) == {"A": 15.0, "B": 100.0}

Fill the gaps

Fill the gaps.

Test plan

I use so a shared total shows. The expected value comes from , and the assert checks .

Try it

Your steps, about 2 minutes

0 of 1
  1. 1.Trace a small, mixed example by hand for one function an AI wrote for you, and turn it into a test. Add "trace one mixed example" to your kit.2 min

Practice

Question 1 of 2

You're writing a test for an AI-written function. Where should the expected value come from?

AI tutor

Ask about this lesson and get coached through the Try it task. The tutor is part of Learn Pro.

See Learn Pro