Saltar al contenido

Lección 20 · Reviewing and Testing AI Code

Testing AI-Written Code

Use tests to check Cursor's code: write tests first, cover boundaries and bad input, find weak tests with a mutation check and give the agent a clear target.

Reading code finds some bugs. Running it against well-chosen tests finds others. Tests also give the agent a goal it can check: "make these tests pass without changing them" is much clearer than "make it work".

Tests first

  1. Write or review tests that describe the behavior you want.
  2. Run them and see them fail. A test that already passes cannot show that your change did anything.
  3. Ask the agent to implement the behavior without editing the tests.
  4. Run them again and read the diff.
TEXT
The tests in tests/test_duration.py describe parse_duration().
They fail now. Implement it in utils/duration.py.
Do not edit the tests. If a test seems wrong, stop and tell me.

You can ask the agent to draft tests too. Review them before the implementation exists. When the same model writes both at once, the tests may simply repeat its own misunderstanding.

Choose cases on purpose

For a function that reads input, think in groups:

  • Normal: "1h30m"
  • Boundaries: "0m", large numbers
  • Bad input: "", "abc", "30x", "30m1h" (wrong order)
  • Tricky: spaces, decimals, negative signs
Python Table-driven tests with clear cases

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The tests also force decisions: is "90m" valid? Should there be an upper limit? Write the answers down as tests. More about test tools is in Python testing.

Are the tests strong enough?

A test suite can pass while the code is wrong. Mutation testing tests the tests: you plant small deliberate bugs and see whether any test fails. If all tests still pass, they missed something. Here is a tiny hand-made version:

Python A tiny mutation check

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weak tests use only comfortable middle values. Adding the exact boundaries 12 and 65, and a member case, catches all three planted bugs. Mutation testing tools automate this idea. Catching every planted bug still does not prove the code is correct.

Watch for shortcuts

When the goal is "make the tests pass", check that an agent did not take a shortcut such as:

  • Returning fixed answers for the exact test inputs.
  • Skipping or deleting a failing test.
  • Catching an error so that nothing fails.
  • Loosening an assertion.
  • Changing test settings so that fewer tests run.

Compare the number of tests that ran before and after, and read changes to test files and test configuration first. A CI rule or code owner setting can require review when they change.

Test beyond units

Level Checks Example
Unit One function The duration parser
Integration Parts working together An endpoint with a test database
End to end A user flow Submitting a form in a browser

The agent's browser tool can open pages, click and take screenshots, which helps with interface checks. The documentation warns against letting browser actions run automatically on untrusted sites. A screenshot that looks right still does not test everything.

Let automation repeat the checks

Run tests, the type checker and the linter in continuous integration (CI) for every pull request. CI runs the same checks no matter who wrote the code. Protect your main branch so that failing checks block a merge.

Review a generated test

When an agent drafts tests, read each one with four questions:

  1. Would it fail if the feature were broken? Try breaking the feature.
  2. Is the expected value independent? An expected value computed by calling the code under test proves nothing.
  3. Is it deterministic? Current time, random numbers, network calls and test order can make a test pass only sometimes.
  4. Is the name honest? A test called "rejects bad input" should really send bad input and check the rejection.

Be wary of tests that replace nearly everything with fake objects. They may only check that the code calls the fakes in the way the test expects. Use fakes for slow or risky outside services, and keep some integration tests that use the real parts together in a safe test environment.

When the existing project has no tests

Do not ask for a large refactor first. Start by capturing current behavior with a few tests around the code you must change. These are sometimes called characterization tests. They record what the code does today, even where that looks odd, so you notice if your change alters it. Mark doubtful behavior with a comment, and decide separately whether it is a bug.

Common mistakes

  • Trusting "all tests pass" without checking which tests ran.
  • Writing tests that copy the implementation's logic.
  • Testing only the sample input from your prompt.
  • Never seeing a new test fail.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Why run a new test before implementing the behavior?

    A test that already passes cannot show that your change did anything.
  2. 2. What does a surviving mutant tell you?

    A missed deliberate bug usually points to a gap in the tests.
  3. 3. Which is a sign that tests were gamed?

    Hard-coded answers pass tests without solving the problem.

Summary

  • Tests give both you and the agent a checkable goal.
  • Review tests before the implementation, and see them fail.
  • Cover boundaries and bad input.
  • Check test strength and watch for shortcuts.

Next: Agent Review and Bugbot

Tu progreso se guarda en este navegador. No necesitas una cuenta.