Take expected results from the requirement

Módulo 1: Test with AI, and prove every test works

Temario del curso

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

Prueba rápida

Guess first, no penalty. You ask an AI to write unit tests for an existing price-rounding function. All tests pass, but customers report wrong totals. What most likely happened?

The code can't tell you what's correct

A unit test checks one small piece of code, such as one function. When you ask an AI tool to "write tests for this function", it reads the function and describes it. If the function has a bug, the test expects the bug, and the suite goes green on wrong behavior.

So the expected result in every test must come from outside the code you're testing:

  • The requirement or acceptance criteria.
  • A worked example you calculate by hand.
  • A person who knows the rule, such as the product owner or someone in finance.

Give the AI the rule, not just the code. Then it writes tests against what should happen. If the rule isn't written down anywhere, that's the first thing to fix, and it's a question for the team, not for the AI.

A worked example

A prompt (the message you send to the AI):

Write pytest tests for round_price() below.

The rule, from the finance team: round to 2 decimal places, halves
round up. So 2.345 becomes 2.35 and 2.344 becomes 2.34.
Take every expected value from this rule, not from the code.
Include a negative price and zero.
If the code disagrees with the rule, say so. Don't change the test.

[paste the function]

Completa los espacios

Fill the gaps.

Prompt

Write tests for round_price(). The rule: . Take every expected value from . If the code disagrees with the rule, .

Try it

Tus pasos, unos 2 minutos

0 de 1
  1. 1.Pick one function your team tests. Find its rule in a requirement or ask someone, write it in one line, and give it to the AI with the code. Add "expected values come from the rule" to your AI test kit.2 min

Práctica

Pregunta 1 de 2

Where should the expected value in a test for an AI-written function 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