Catch the bug that looks right

Módulo 1: Code with AI, and check every line

Temario del curso

Esta lección aún no está en tu idioma, así que aquí la tienes en inglés.

Encuentra los errores

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 de 1 encontrados

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}

Completa los espacios

Fill the gaps.

Test plan

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

Try it

Tus pasos, unos 2 minutos

0 de 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

Práctica

Pregunta 1 de 2

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

Tutor de IA

Pregunta sobre esta lección y recibe ayuda con la tarea de “Pruébalo”. El tutor es parte de Learn Pro.

Ver Learn Pro