AI Made Simple

When AI Writes Your Code, Tests Are Your Expectations

Turn your requirements into tests before the code exists, and avoid letting the AI mark its own homework.

AI Made Simple · 4 min read

If an AI writes code for you, you may not be able to read every line. Tests let you state what the code must do, then check it automatically.

Code is a precise instruction

Ordinary language leaves room for interpretation. Code does not, so an AI that writes code from a loose description may choose an interpretation you did not intend. A test is a small, concrete statement of what you expect: given this input, produce that output.

Write expectations first

Before asking for code, list a handful of examples, including the awkward ones: an empty input, a very large value, a repeated item. These become tests. When the AI writes the code, you run the tests. Passing does not prove the code is perfect, but failing tells you immediately that it is wrong.

Do not let the AI mark its own homework

If the same assistant writes both the code and the tests, they can share the same misunderstanding. Review the tests yourself, and add cases of your own. Ask the AI to explain what each test protects against.

Other checks worth making

  • Read the dependencies the code asks you to install, and confirm they are real and well known.
  • Keep secrets such as passwords and keys out of prompts and out of code.
  • Run new code in a safe test environment first.
  • Keep a record of what you asked and what changed.

More guides from this book