Agents That Write Tests: Generating Unit Tests With LLMs in CI

AI agents read coverage reports, write unit tests for uncovered lines, run them, and open a pull request. Here is how to wire one into CI safely.

By Galactic News Editorial · Published 2026-11-26T17:09:17+00:00 · Updated 2026-11-26T17:09:17+00:00

AI agents writing unit tests generating unit tests with LLMs in CI

Every team has the same gap. New code ships faster than tests for it. Coverage falls. Bugs reach production. AI agents now close that gap. They read the changed code, write unit tests for it, run them, fix the failures and open a pull request. The developer reviews the result. The pipeline keeps moving.

This guide explains how AI agents writing unit tests work, which tools do it well, and how to wire one into CI without losing control. The rule throughout: generated tests are drafts until a human reviews them and the suite passes.

How AI agents writing unit tests work

The pattern is the same across most tools. It has four steps. First, the agent finds the gap. It reads the coverage report and lists the lines the tests do not cover. Second, it writes tests for those lines. It studies the existing test files to learn the project's style. Third, it runs the tests. It fixes failures and repeats until the tests pass or it hits a limit. Fourth, it opens a pull request with the new tests.

Qodo Cover is the best-known open source version of this loop. It is built on the first open source implementation of Meta's TestGen-LLM. It analyses source code, generates tests, validates that each test runs, passes and increases coverage in a meaningful way. It supports Python, Go and Java. It runs from the command line or as a GitHub Action in CI.

The loop has a quality filter. A test only counts if it builds, passes and raises coverage. A test that passes without covering new lines is rejected. A test that covers lines but asserts nothing useful is a weaker gain. The agent repeats the cycle until it reaches the coverage target or the maximum number of iterations.

Meta's original TestGen-LLM paper shows the approach works at scale. Over three test-a-thons, it improved 196 of 1,979 test classes it was applied to. That is about 10 percent. The lesson is honest. The agent does not fix every gap. It fixes a steady share of them, and it does so without a developer typing each test by hand.

The tools that generate unit tests in CI

Several tools now offer this loop. They differ in how autonomous they are and where they run.

ToolHow it runsLanguagesWhat it does in CI
Qodo CoverCLI or GitHub Action, open sourcePython, Go, JavaGenerates tests for uncovered lines, validates them, opens a follow-up PR
TestSpriteIDE and MCP-native agentMultiple languagesAutonomous generation, execution, failure classification and self-healing
Diffblue CoverCLI and CI integrationJava and othersGenerates regression tests and runs them in the pipeline
GitHub CopilotIDE extensionMany languagesGenerates tests on demand in the editor, not in CI

Qodo Cover is the practical starting point. It is open source under AGPL-3.0. It uses your own LLM API key. A generated pull request with 15 unit tests was accepted into Hugging Face's PyTorch Image Models repository, covering 168 new lines of code. That is a real result on a real project with more than 30,000 GitHub stars.

TestSprite leads on full lifecycle work. It classifies failures, heals flaky tests and feeds structured feedback back to the coding agent. Diffblue Cover focuses on regression tests that lock in current behaviour. GitHub Copilot is useful for writing tests by hand in the editor. It does not run the CI loop on its own.

Check the licence before you adopt an open source tool. Qodo Cover is AGPL-3.0. That licence requires source disclosure for networked use of modified versions. Read the terms with your team's legal guidance if you plan to modify it.

Wiring test generation into your CI pipeline

The safest design keeps the coverage gate strict and lets the agent draft the fix. When coverage drops below your threshold, the merge is blocked. The agent opens a pull request with the missing tests. A developer reviews every one. Nothing merges without a human.

Build it in steps:

  1. Enforce the coverage gate first. Configure branch protection so the quality job must pass before anything merges.
  2. Add a job that builds the code, runs the tests with coverage, and reads the coverage from your scanner.
  3. Add a second job that runs only when coverage is below the threshold.
  4. That job lists the uncovered lines and asks the LLM to write tests for them.
  5. Run the new tests. Send failures back to the model for a fix, up to a fixed number of attempts.
  6. Open the pull request against the feature branch, never against main.
  7. Post the coverage report as a comment on the pull request.

Keep the agent's pull request separate from the developer's. A dependent pull request targeting the feature branch is the right shape. The developer merges the tests into their branch, the coverage gate re-runs, and the merge unblocks. The main branch never receives unreviewed test code.

Scope the agent tightly. Start with pure functions that have no input or output. Pilot it on five to ten functions in a utilities module. Exclude generated code, schema declarations and boilerplate with an ignore file. A team that built this loop in GitLab CI used a single TypeScript script of about 700 lines. It ran on every push to staging, diffed the commit, generated tests, fixed failures and opened a merge request.

Never let test generation block a deploy. If the model API is down or the job fails, the pipeline should keep moving and notify the team separately. The agent is a helper, not a gate.

What agent-written tests get wrong

Generated tests have known weaknesses. Plan for them. First, a test can pass while testing nothing. It calls the function and asserts a trivial result. Guard against this by requiring each test to cover new lines and to fail when the code is deliberately broken. Qodo Cover's newer agent uses this mutation-style check.

Second, a test can lock in a bug. If the code is wrong and the test asserts the wrong behaviour, the suite now protects the bug. That is why a human must review every generated test. The reviewer asks one question. Does this test describe what the code should do, or what it happens to do?

Third, research from February 2026 urges caution. A paper titled Rethinking the Value of Agent-Generated Tests found that tests written during agent work differ in value from tests written for CI. The finding supports a clear practice. Keep agent-time tests and CI tests separate. Let the agent draft. Let the CI suite hold only reviewed tests that assert real behaviour.

Fourth, flaky tests cost more than missing tests. A generated test that depends on the clock, on random data or on network order will fail at random. Review for those dependencies. Delete a flaky test rather than keeping it. A suite developers stop trusting is worse than a suite with lower coverage.

Bottom line

AI agents that write unit tests are worth adopting, with the right shape. Keep the coverage gate strict. Let the agent draft the missing tests. Require the tests to run, pass and raise coverage. Open them as a pull request to the feature branch. Review every one before it merges. Start small, on pure functions, and grow the scope as the reviews stay clean. The agent writes the boilerplate. The developer keeps the judgement. That division is what makes the loop work.