Skip to content
Technology, explained from the insideAI · Software · Digital payments
TayoTechTalk.

AI Software Development

How to Test AI-Generated Code Without Repeating Its Mistakes

By Temitayo Solomon Gbadebo · November 5, 2026

An AI coding assistant produces a function, adds a test file and reports that everything looks good. That feels like a complete delivery. But there is a difficult question underneath: did the tests check the requirement, or did they simply repeat the same misunderstanding as the code?

After establishing a sensible AI-assisted development workflow, the next step is learning how to challenge the result. Tests are most useful when they provide evidence that could prove the implementation wrong.

Start with a rule that exists outside the code

Consider a small online shop with an inventory reservation feature. A customer asks to reserve three items when five are available. The expected result is a successful reservation and two items remaining.

Before asking an assistant to implement this, write down the rules:

  • The requested quantity must be a positive whole number.
  • A reservation cannot consume more stock than is available.
  • A failed reservation must not reduce available stock.
  • A customer cannot change another customer’s reservation.
  • If a request is retried with the same operation identifier, it must not reserve stock twice.

These are illustrative requirements for this example, not rules that every shop already implements. Your product needs its own agreed behaviour, including what happens when a reservation expires or is cancelled.

Keep that short specification visible while reviewing both code and tests. Otherwise, the implementation can quietly become the definition of correctness.

Ask for a test plan before asking for test code

A useful prompt is: “Given these reservation rules, list the normal cases, boundary cases and failure cases. State the expected result for each. Do not assume the implementation is correct.”

Review the proposed cases yourself. An assistant may suggest useful scenarios, but it can also omit the awkward ones. GitHub’s responsible-use guidance explicitly recommends reviewing and testing generated code, with particular care around security-sensitive applications.

For the inventory example, test reserving one item, all five items and six items. Test zero, a negative quantity, a fractional quantity and an absent quantity. Decide whether the input boundary rejects strings or converts them, then test that decision. Ambiguity should become an explicit requirement rather than an accidental behaviour.

Check the result and the side effects

A weak test might only check that the reservation function returns “success.” A stronger test also checks the remaining stock, the saved reservation and the customer associated with it.

For a rejected six-item request, inspect the state afterwards. Five items should still be available. No successful reservation should have been recorded. A correct error message cannot compensate for an incorrect database update.

Assertions express those expectations in executable form. The pytest assertion documentation shows how Python tests compare actual and expected values and verify expected exceptions. Other languages have equivalent tools. The important part is the behaviour being asserted, not the testing framework’s brand.

When possible, calculate expected values from the requirement rather than calling the same helper function used by the implementation. Otherwise, one faulty calculation can appear on both sides of the comparison and make a broken result pass.

Use properties as well as individual examples

Example tests ask what should happen for specific inputs. Property-based tests ask whether a rule holds across many generated inputs.

For a successful reservation, a useful property is that remaining stock equals starting stock minus the accepted quantity. For rejected requests, stock should remain unchanged. Another property is that available stock never becomes negative.

Hypothesis, a property-based testing library for Python, lets developers describe input strategies and assertions. It can explore cases beyond a small hand-written list. This is useful for discovering combinations a developer or coding assistant did not think to specify individually.

Properties still need care. “The function does not crash” is too weak for an inventory system. A program that silently accepts every order could satisfy it while overselling the shop. The rule must capture something the business actually relies on.

Test the boundaries between components

Unit tests can check a small piece of logic quickly. They do not necessarily tell you whether the API, database transaction and permission checks work together.

Add integration tests for the reservation flow in a controlled test environment. Check that an unauthorised customer cannot access another customer’s reservation. Check what remains in the database when a later step fails. Keep production credentials and customer data out of the test setup.

Concurrency deserves a deliberate test. If only one item remains and two customers request it at nearly the same time, the system should not confirm both reservations. Two successful tests run one after another will not demonstrate that protection. The test needs overlapping requests and a reliable check of the final state.

Likewise, a fake email or payment service is useful for controlled scenarios, but passing against a fake proves only the behaviour modelled by that fake. Separately check the real integration contract using the provider’s supported testing facilities.

Check whether a test can catch a known mistake

In an isolated development branch, deliberately introduce a small, obvious error. For example, make the reservation subtract one extra item. Run the relevant tests. A test intended to verify stock arithmetic should fail.

Restore the correct code afterwards. This small exercise is a practical way to challenge the tests themselves. If nothing fails, investigate whether the test is missing, skipped or checking the wrong thing.

You can also remove a permission check temporarily in that isolated environment and confirm that the access-control test catches the change. Never perform this experiment in production or with real customer accounts.

Read the test run, not just the assistant’s summary

Ask for the exact command that ran, its result, and any skipped tests or environment limitations. A test file being created is different from a test suite being executed.

For example, pytest distinguishes successful execution from failure and from collecting no tests. A report that says “no failures” can be misleading if no tests were discovered in the first place.

Check that the run used the current code. If a dependency or database prevented execution, record that gap plainly. Do not turn “could not run” into “passed.” Coverage reports can show which code was exercised, but they do not establish that the assertions captured the correct requirements.

A practical review standard

Before merging an AI-assisted change, ask: are the requirements clear, do the tests cover important failures, did the tests actually run, and has someone reviewed the behaviour the tests cannot establish?

AI can help write tests, suggest edge cases and explain failures. The developer’s responsibility is to make sure those tests challenge the implementation. Confidence should come from observable evidence and sound review, rather than from how confidently the assistant describes its own work.