How AI Agents Can Write and Run Tests
How AI coding agents generate test cases and use test results as feedback, and where their test-writing instincts need a second look.
Generating tests from existing code
Given an existing function, an agent can read its signature and body, infer plausible inputs, and generate assertions for the outputs it observes when it actually runs the code. This is a genuinely useful way to backfill coverage on code that currently has none.
The catch is that tests generated purely by observing current behavior will faithfully encode bugs as if they were correct behavior. A test suite built this way documents what the code does, not necessarily what it should do, so it is only as trustworthy as the code it was generated from.
Using tests as a feedback loop
Where agents are genuinely strong is using an existing test suite as a steering signal while making changes: write code, run the tests, read the failure, adjust, repeat. This loop lets the agent converge on a correct implementation without a human in the middle of every iteration.
This only works as well as the tests themselves. A weak or shallow test suite gives weak feedback, and an agent will happily produce code that passes weak tests while still being wrong in ways the tests never checked for.
- Strong: backfilling coverage on existing, unchanged code
- Strong: using tests as a loop to converge on working code
- Weak: writing tests for new, unbuilt behavior with no spec
- Weak: catching edge cases the existing test suite never considered
- Risk: encoding existing bugs as if they were the correct behavior
What to still do yourself
Review AI-generated tests as carefully as AI-generated implementation code. A test that always passes regardless of the implementation is worse than no test, because it creates false confidence. Check that assertions are meaningful and that at least one edge case beyond the happy path is covered.
For genuinely new behavior, write the specification or the key test cases yourself before asking the agent to implement against them. This gives the agent the same clear feedback loop it thrives on for existing code, instead of asking it to invent both the spec and the implementation at once.
Key takeaways
- Apply one concrete change from this post before collecting more reading.
- Prefer browser-side tools when the work involves secrets, tokens, or PII.
- Document the why next to the how so the next reviewer inherits context.
FAQ
- Who is this guide on ai for?
- Working developers who need a practical take on how ai agents can write and run tests — not a marketing overview. Skim the sections, apply one tip, then come back when you hit an edge case.
- Do I need an account to use the related tools?
- No. code.live tools run in your browser with no signup. Nothing you paste is uploaded to a server for the client-side utilities linked from this post.
- How often is this article updated?
- This post was published October 28, 2026. Fundamentals stay stable; check linked tool pages and official docs when version-specific behavior matters.