Back to blog
Software Development

Testing AI-Generated Code: Why "It Works" Is Not Enough

8 December 2026
Testing AI-Generated Code: Why "It Works" Is Not Enough

AI-generated code usually runs. It often passes the tests the AI wrote for it. Neither of those tells you it's correct, secure or maintainable.

The self-marking problem

If the same model writes the code and the tests, the tests tend to repeat the model's own assumptions. A misunderstood business rule gets implemented incorrectly and then "verified" as correct. Tests need to come from the specification, not from the implementation.

A layered verification approach

  • Acceptance tests from the specification. Write them from the agreed acceptance criteria, ideally before implementation begins or independently of it.
  • Negative and misuse tests. What happens with invalid input, missing permissions or duplicate submissions? Does access control hold when someone calls the API directly?
  • Static analysis and SAST. These catch common vulnerability patterns and code-quality problems.
  • Software composition analysis. This checks every dependency for known vulnerabilities and licence issues.
  • Secret scanning, before code is committed and in the pipeline.
  • Dynamic testing (DAST) against a running test environment.
  • Human review focused on logic, design and security, not formatting.

Review that adds value

Reviewers should ask:

  • Does this match the specification?
  • Is authorisation checked on the server?
  • What happens when this fails?
  • Could someone maintain this in two years?

Big AI-generated pull requests should be split up. Nobody can review 3,000 lines properly.

Maintainability matters

AI can produce duplicated, inconsistent or over-complicated code. Track duplication, complexity and test coverage over time. Clean-looking code that nobody understands is still a liability.

Buyer's checklist

  • Are acceptance tests written independently of the AI-generated code?
  • Do tests cover misuse and access control?
  • Which security scans run on every change?
  • Are pull requests kept to a size people can review?

How Cyber Samurai can help

Further reading in this series

Keep reading

Recommended articles