Can an AI Agent Write Unit Tests That Catch Real Bugs?
Quick answer: Yes, if it is aimed at the right code and held to the right standard. An AI agent writes useful unit tests when it picks logic where a mistake would be costly and nothing would notice, reads that code until it can say what it should do, tests behaviour rather than implementation, and proves each important test fails when the code is broken on purpose. Tests written to raise a coverage number, or that only restate what the code does today, protect nothing.
Let an agent earn its place on your team
Define a Factorik agent once and it wakes on an issue, a failed build or a schedule, works in an isolated sandbox and ships a pull request. It starts with a person merging its work and earns more autonomy on its record. Try it free for 7 days, no payment details needed.
Why do AI-written tests so often protect nothing?
Ask a coding assistant to "add tests for this file" and you usually get tests that pass. That is the problem. A test that is written by reading the current code and asserting what it does today will pass whether the code is right or wrong. It records behaviour; it does not check it. If the function rounds a refund the wrong way, the generated test asserts the wrong rounding, and from then on it defends the bug.
The second failure is choosing what to test by what is easy. Getters, mappers and thin wrappers are simple to cover and raise the coverage number quickly. The code that actually hurts when it breaks, the branchy function that decides what a customer is charged or who may see a record, is harder to set up, so it gets left for later.
Neither problem is specific to AI. People write tautological tests and chase coverage too. An agent just does it faster and at a larger scale, so the standard it is held to has to be written down: what to test first, how to know a test is worth keeping, and what to do when writing a test reveals a bug.
- +Chosen where a mistake would be costly
- +Assert what the code should do, from reading its callers
- +Cover edges: empty, zero, maximum, malformed input
- +Shown to fail when the code is broken on purpose
- −Chosen because they were easy to write
- −Assert whatever the code returns today
- −Only the happy path
- −Never seen failing, so nobody knows they can
What code should an AI agent write tests for first?
Code where a mistake is costly and nothing would notice it. In most applications that means money (prices, discounts, refunds, invoices), permissions (who may read, change or delete what), data that is written or deleted, parsing of input that comes from outside, and anything with many branches.
Two more signals sharpen the list. If the team has an error tracker connected, code that has produced real errors in production is a strong candidate: it has already shown it can go wrong. And git history shows which files change often; code that changes often and has no tests is where regressions come from.
This is how Factorik's Test Writer preset works. It is a ready-made agent whose job is to improve a repository's tests, with the goal written into its instructions as tests that would catch a real mistake, not a higher coverage number. By default it runs on a weekly schedule with a task to find the most important untested logic and cover it, and it works better when it can read an error tracker. Each run covers one area, and its next run reads what it did recently so it moves on rather than starting over.
How does an agent know what the code is supposed to do?
By reading more than the function. The function shows what the code does; its callers, its types, its name, the comments and documentation, the issue that introduced it and the tests next to it show what it is meant to do. An agent working in a full clone of the repository can read all of that, which is the main advantage it has over a suggestion in an editor that sees one file.
Sometimes the intended behaviour is not clear even after reading. Should a discount code that expired at midnight still apply at 00:00:30 in the customer's time zone? A test that picks an answer turns a guess into a specification. The Test Writer's instructions are direct about this: if the intended behaviour is unclear, pick something else and mention it. The question goes to a person in the run's summary instead of being settled silently in a test file.
A team can make this better over time. "Rules from the team" on the agent holds standing rules every run follows, written only by people, such as "tests for the billing module use the fixtures in test/fixtures/billing" or "never mock the database in integration tests". Feedback on a run, with "Make this a standing rule", turns a correction into a rule the next run reads.
How do you prove a generated test can fail?
Break the code and run the test. If the test still passes, it protects nothing. This is the idea behind mutation testing, done by hand for the tests that matter most: change a comparison from less-than to less-than-or-equal, drop a branch, return early, and check that at least one test turns red. Then put the code back.
For a person this step is tedious, which is why it is usually skipped. For an agent working in an isolated sandbox it is cheap: it can change the code, run the suite, read the result and restore the file in a few commands, and nothing it does there reaches the repository unless it is part of the final change. The Test Writer is told to do exactly this for its most important tests.
The same habit catches the other common failure: a test that cannot fail because it mocks the very thing under test, or asserts on a value it set up itself. A test that never turned red under a deliberate break is a test to delete or rewrite before the pull request is opened.
What should happen when writing a test uncovers a bug?
It should be reported, not fixed in the same pull request. A pull request titled "Add tests for coupon validation" that also changes how coupons are validated is a behaviour change hiding behind a test change. A reviewer skimming test files will not look for it, and the change history will not say it happened.
The Test Writer's instructions say not to change behaviour: if it finds a bug while writing tests, it leaves it and describes it in its summary with the failing case, so a person can decide. A person can then hand that case to a Bug Fixer, which writes its own failing test and fixes the cause in a pull request that says so.
This separation is also what keeps a test agent's pull requests small and easy to review. A change that only adds test files, touches no protected path and passes its checks is the kind of change that can qualify as low-risk under a merge policy, once the agent has earned the level that lets it merge those by itself.
Let an agent earn its place on your team
Define a Factorik agent once and it wakes on an issue, a failed build or a schedule, works in an isolated sandbox and ships a pull request. It starts with a person merging its work and earns more autonomy on its record. Try it free for 7 days, no payment details needed.
What does one run of a test-writing agent look like?
An illustration with made-up names. A team adds a Test Writer to acme/shop, connects Sentry to it, and leaves it at the first level, where a person merges what it opens.
On Wednesday morning its schedule wakes it. In its sandbox it lists files with no tests and cross-checks recent Sentry errors. src/orders/refund.ts has no tests, changed eleven times in two months, and produced a handful of errors last week. It reads the function, the two callers, and the issue that introduced partial refunds.
It writes tests for a full refund, a partial refund, a refund on an order with a discount, a refund of zero, a refund larger than the order, and a currency with no minor unit. Then it breaks the rounding on purpose: two tests turn red. It restores the code. One case, a partial refund on a discounted order, returns a cent more than the discount rules in the issue describe. It does not change refund.ts.
It finishes. The server publishes one commit on a new branch with one test file and opens a pull request. The summary lists what is covered, the deliberate break and which tests caught it, and the suspected bug with the input that shows it. CI runs; all checks pass. An engineer reads the tests, presses "Approve and merge" in the Inbox, and opens an issue for the rounding case, labeled for the Bug Fixer.
How is this different from asking an assistant for tests in the editor?
Both use a model to write test code. The differences are in what surrounds it. An editor assistant writes what you ask for, in the file you have open, and you decide whether to keep it. A test-writing agent decides what to test, on a schedule, across the whole repository, and its work arrives as a pull request with checks, a review and a merge policy in front of it.
That makes the agent useful for the backlog nobody gets to, and the editor assistant useful while you are already in the code. They do not compete. What matters for the agent is that it is held to the same standard a careful reviewer would hold a person to, and that its record decides how much it is trusted. In Factorik that record is per agent: how many of its pull requests merged, how many needed changes, and whether any merged change later caused a problem, which moves it down a level at once.
It also has limits. An agent cannot know a business rule nobody wrote down, cannot judge which flows matter most to customers unless it can see usage, and will write weaker tests in a repository with no test setup to follow. It is one more contributor to the test suite, not a replacement for people deciding what correct means.
How do I start with a test-writing agent?
Pick one repository with a working test command and CI. Create an agent from the Test Writer preset, connect your error tracker if you have one, and add a rule or two about your test conventions. Keep "Merge automatically" off in the repository's merge policy, so every pull request waits for a person while the agent builds a record. Read its first few pull requests closely, especially the deliberate-break notes, and give feedback on what it chose to cover.
The same preset sits alongside the other maintenance agents, such as the Dependency Updater and Maintenance Engineer, each with one narrow job, and the CI Fixer for when a build breaks. If you want to try it on your own code, start a free 7-day trial of the Team plan, with no payment details needed.
Frequently asked questions
Can AI write good unit tests?
It can, when it chooses code where a mistake is costly, reads callers and history to learn what the code should do, tests behaviour and edges, and shows each important test fails when the code is broken on purpose. Without those habits it tends to write tests that only restate the current code.
Should an AI test agent aim for higher code coverage?
Coverage is a side effect, not the goal. A few tests on billing, permissions and input parsing that are shown to fail under a deliberate break protect more than many tests on simple code.
What if the agent finds a bug while writing tests?
It should leave the code as it is and report the bug with the failing case, so a person decides. Fixing it inside a test pull request hides a behaviour change from reviewers.
Does the Test Writer merge its own pull requests?
Not at first. Every agent starts at the level where a person merges what it opens. It can earn more autonomy on its record, and only inside the repository's merge policy.
Let an agent earn its place on your team
Define a Factorik agent once and it wakes on an issue, a failed build or a schedule, works in an isolated sandbox and ships a pull request. It starts with a person merging its work and earns more autonomy on its record. Try it free for 7 days, no payment details needed.