Take expected results from the requirement

Module 1 : Test with AI, and prove every test works

Plan du cours

Cette leçon n'est pas encore dans votre langue, la voici donc en anglais.

Petit test

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]

Complétez les trous

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

Vos étapes, environ 2 minutes

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

Exercices

Question 1 sur 2

Where should the expected value in a test for an AI-written function come from?

Tuteur IA

Posez vos questions sur cette leçon et faites-vous guider dans l'exercice « Essayez ». Le tuteur fait partie de Learn Pro.

Voir Learn Pro