AI Test Generation
AI test generation promises to erase the most tedious part of shipping software — writing the tests. But most tools just autocomplete plausible-looking test code from the shape of a function signature, and plausible is not the same as correct. This guide explains how AI test generation actually works, why runtime-grounded tests beat hallucinated ones, and how a deterministic compile-run-repair loop turns a language model's first draft into a suite that is green before it ever reaches your editor.
- What Is AI Test Generation?
- Runtime-Grounded Tests vs Hallucinated Ones
- How AI Test Generation Works: Analyze, Seed, Generate, Verify
- Deterministic, Self-Repairing Generation: Compile → Run → Repair
- Unit, Flow, and Service-Level Tests
- Coverage That Counts: Line + Executed-Flow
- Multi-Language Test Generation, Idiomatic to Each Stack
- Seeding Tests From Real Runtime Values
- How SlicerCode Generates Tests End to End
What Is AI Test Generation?
AI test generation is the use of a large language model to produce runnable test code — unit tests, integration tests, or end-to-end flows — from your existing source. Instead of you hand-writing every fixture, mock, and assertion, the model reads a function, class, or entry point and emits a test file in your project's framework: JUnit for Java, pytest for Python, Jest or Vitest for TypeScript, and so on. Done well, test generation collapses hours of boilerplate into seconds and raises coverage on code that would otherwise go untested.
The catch is that a naive generator only sees *syntax*. It knows the function is called transfer(from, to, amount) and it knows the surrounding types, but it has never watched the function run. So it invents inputs, guesses what a downstream service returns, and writes an assertion like assertEquals(200, response.status) because 200 is the statistically likely answer — not because your code actually returned it. That is the difference between a test that documents real behavior and one that merely looks like a test.
High-quality test generation closes that gap by grounding the model in facts about your program: its real call graph, the concrete values it produces at runtime, and the frameworks and conventions already in your repo. The rest of this guide walks through how that grounding works, why it matters, and how it changes the output from something you have to babysit into something you can trust.
- Input: a function, class, flow, or service entry point
- Output: idiomatic, runnable tests in your project's framework
- The differentiator is grounding — real values and structure, not signature-shaped guesses
Runtime-Grounded Tests vs Hallucinated Ones
A hallucinated test compiles, reads well, and asserts the wrong thing. Because the model never observed the code, its assertions encode the *expected* behavior it inferred from names and types — which is exactly the behavior a real test is supposed to independently verify. When the assertion and the implementation are both authored by the same guesser, a passing test proves nothing, and a subtly wrong golden value quietly enshrines a bug as the expected result.
A runtime-grounded test inverts this. Before generating, the system captures what the code *actually did* on a real execution: the concrete arguments each function received, the object state as it mutated, and a best-effort return value at each step. Those observed values become the assertions. Now the test says assertThat(balance).isEqualTo(new BigDecimal("142.50")) because 142.50 is the number your code genuinely produced under a real request — a fact worth pinning down, not a plausible-sounding placeholder.
This also fixes the fixture problem. Hallucinated mocks return whatever the model imagines a repository or HTTP client hands back; grounded mocks replay the shapes that were really observed on the wire. The result is a test that fails when behavior *changes*, which is the only kind of failure a regression test exists to catch. SlicerCode captures these values by attaching to the debugger — see Runtime Analysis & Dynamic Tracing for how that capture works and how secrets and PII are redacted before anything leaves the IDE.
- Hallucinated: assertions and implementation share one guesser — a green test proves nothing
- Grounded: assertions come from observed runtime values and object shapes
- Grounded mocks replay real return shapes instead of imagined ones
How AI Test Generation Works: Analyze, Seed, Generate, Verify
Robust test generation is a pipeline, not a single prompt. The first stage is static analysis: parse the codebase, build a call graph for the target, and determine its data- and object-flow so the generator knows what the function touches, what it depends on, and where external systems (a database, a queue, an HTTP call) sit on the path. This is the same flow model described in Code Flow & Graph Analysis, and it tells the generator exactly what needs mocking versus what can run for real.
The second stage is seeding. When a runtime capture exists, the pipeline pulls the concrete arguments, object state, and observed returns for the target and hands them to the model as ground truth. Seeding is what lets the generator write realistic inputs and exact expected values instead of foo, 42, and "test". Without a capture, generation still works — it just leans harder on static structure and produces assertions you should review more closely.
The third stage is generation: the model, now armed with structure and real values, writes the test file in your detected framework and project layout. The fourth and most important stage is verification — the generated test is compiled and executed, and if it does not go green, it is repaired. A generator that stops at stage three is handing you a draft; one that runs stage four is handing you a verified test. The next section covers that loop in detail.
- Analyze: call/data/object flow reveals dependencies and external systems to mock
- Seed: real captured values become inputs and expected assertions
- Generate: idiomatic test in the detected framework and layout
- Verify: compile and run — a draft becomes a test only after it passes
Deterministic, Self-Repairing Generation: Compile → Run → Repair
The single biggest reason to distrust AI-written tests is that they often do not run. A missing import, a wrong mock signature, an assertion off by a type — any of these turns a generated file into homework. The fix is to treat *every* generation as something to execute, not just something to write. In a compile-run-repair loop, the freshly generated test is compiled; if compilation fails, the errors are fed back to the model to fix. Then it runs; if it fails at runtime, the stack trace and failure output are fed back and repaired. The loop continues until the suite is green.
This is what "self-repairing" means in practice: the model diagnoses its own broken imports, wrong mock setups, and mis-typed assertions using the compiler and test runner as an oracle, rather than shipping the error to you. Because a compiler is a deterministic checker, the loop converges on tests that genuinely pass in *your* environment — with your dependencies, your JDK, your test framework — not tests that merely looked right in the model's head.
"Deterministic" refers to reproducibility: given the same source and the same captures, the pipeline aims to produce the same verified output rather than a different lucky roll each time. SlicerCode runs exactly this loop — it compiles, runs, and repairs assertions, imports, and mocks until green, and it never hands you a red test to fix yourself. You can read the feature summary on the Features page and the setup notes (including slicercode.autoSelectJdk for pointing at the right JDK) in the Docs.
- Compile → feed compiler errors back → fix
- Run → feed failing stack traces back → repair assertions, mocks, imports
- Loop until green; reproducible for the same code and captures
- You never receive a red test to fix by hand
Unit, Flow, and Service-Level Tests
Not every gap is a unit-test gap. Effective test generation lets you choose the *altitude* of the test to match what you are trying to protect. At the lowest level are group (unit) tests that cover a single function or a class as a cohesive unit, mocking its collaborators and asserting on its direct outputs. These are fast, focused, and ideal for pinning down a specific algorithm or edge case.
Flow tests step up a level and exercise a path that spans multiple modules and layers end to end — the kind of test that catches an integration bug where each unit is individually correct but they disagree at the seams. Because the generator already knows the call graph and data-flow of the endpoint or job, it can drive the whole path with realistic inputs and assert at the boundaries that matter. Service-level tests go highest, exercising an entire entry point (an API route, a background job) with realistic data, closest to how the system is actually invoked.
SlicerCode generates at all three levels and factors shared setup out automatically, so a service suite does not repeat the same fixture in every case. Choosing the right level is a real skill — you want most tests fast and low, with a thinner layer of flow and service tests over the paths that carry the most risk. Pairing this with PRD Validation lets you target generation at exactly the requirements a flow is supposed to satisfy.
- Group/unit: a function or class as a cohesive unit with mocked collaborators
- Flow: an end-to-end path across modules and layers, driven by the call graph
- Service: a whole entry point exercised with realistic data
- Shared setup is factored out automatically across a suite
Coverage That Counts: Line + Executed-Flow
Coverage tells you whether generated tests are actually reaching the code you care about, but the *type* of coverage matters. Line coverage measures which source lines executed during a run and is produced by mature, language-native tools — JaCoCo for the JVM, coverage.py for Python, c8 for Node, and go cover for Go. It is precise, trusted, and easy to enforce in CI. It is worth being clear that SlicerCode reports line coverage plus executed-flow coverage — it does not claim branch coverage, and no honest tool should conflate the two.
The distinction matters because a line can be covered without every *branch* through it being tested. if (a && b) can hit 100% line coverage while never exercising the case where a is true and b is false. Line coverage is a floor, not a ceiling: high line coverage means untested code is at least visible, but it is not a proof of thoroughness, and treating it as one is a classic coverage trap.
On top of line coverage, executed-flow coverage answers a different, architectural question: of all the stages in this endpoint's flow, which ones actually ran? Overlaid on the flow graph, it lights up covered versus uncovered stages so you can see, for example, that the happy path is exercised but the retry branch of a queue consumer never fired. SlicerCode renders per-flow coverage rings plus an overall scorecard and overlays real line percentages right on the map — turning "is this tested?" into a concrete target you can raise. See it on the Features page.
- Line coverage: which lines ran, from language-native tools
- Executed-flow coverage: which flow stages ran, overlaid on the graph
- Not branch coverage — a covered line can still hide an untested branch
- Per-flow rings + an overall scorecard make coverage a target you can hit
Multi-Language Test Generation, Idiomatic to Each Stack
A test generator that only speaks one language is a plugin; one that speaks your whole polyglot backend is infrastructure. Real codebases mix a JVM service, a Python data job, a TypeScript frontend, and a Go microservice, and each has its own idioms. Generating a pytest test that looks like translated Java helps no one — it has to look like a pytest test a Python engineer on that team would have written.
SlicerCode generates across 6+ languages — Java, Kotlin, Python, JavaScript/TypeScript, Go, Ruby, and more — using tree-sitter to parse each one. It detects your framework and mirrors your layout: JUnit 5 with AssertJ, Mockito, and MockK on the JVM; pytest fixtures, parametrize, and monkeypatch in Python; Jest or Vitest with typed mocks in JS/TS; table-driven tests with t.Run subtests in Go; and RSpec describe/context/it with let in Ruby.
Idiomatic output is not cosmetic — it determines whether the generated suite gets merged or quietly deleted. Tests that match the conventions already in the repo read like they belong, review faster, and are far more likely to be maintained. The multi-language breakdown lives on the Features page, and the frameworks detected per language are listed in the Docs.
- Java/Kotlin — JUnit 5, AssertJ, Mockito, MockK
- Python — pytest fixtures, parametrize, monkeypatch
- JS/TS — Jest & Vitest with typed mocks
- Go — table-driven tests with t.Run subtests; Ruby — RSpec describe/context/it with let
Seeding Tests From Real Runtime Values
The mechanism that makes grounded tests possible is runtime capture. SlicerCode attaches to the VS Code debugger via the Debug Adapter Protocol (DAP), arms breakpoints on the functions in the flow you are targeting, and — as your app runs or as you trigger a route — records each function's concrete arguments, object state as it changes, and a best-effort return value inferred from locals. Secrets and PII are redacted before anything is persisted, and captures are stored locally.
Those captures are what a generated test is *seeded* from. Rather than inventing a User object, the generator reconstructs the one your code actually processed; rather than guessing what a repository returned, it replays the observed shape. This is why runtime-seeded tests carry real golden values and realistic fixtures instead of placeholders, and why they double as executable documentation of how the code behaved on a genuine request.
Capture is gated behind slicercode.allowRuntimeCapture because it executes your code, and it only resumes stops on its own breakpoints so it never hijacks a debugging session you started. To go deeper on how tracing resolves polymorphic call edges to what actually ran and how the debugger integration is scoped, read Runtime Analysis & Dynamic Tracing.
- Attaches to the VS Code debugger (DAP) and arms breakpoints on the flow
- Records real arguments, object shapes, and best-effort returns
- Secrets/PII redacted; captures stored locally; gated by a setting
- Seeds generation with real values so assertions are genuine golden data
How SlicerCode Generates Tests End to End
Putting it together, SlicerCode's test generation runs the full pipeline inside VS Code. On open, it parses your workspace and builds the call, data, and object flow for every endpoint and job. Optionally, you capture a real run to seed generation with observed values. Then you right-click a function — or use the Analyzer panel — and choose Generate tests; the framework is auto-detected, the tests are written idiomatically, and the compile-run-repair loop drives them to green before you see them.
Because the same analyzer also powers the AI coding agent, PRD validation, and coverage, test generation is not a bolt-on — it draws on the exact flow graph and runtime values the rest of the product uses. You can generate straight from a PRD and a flow, then read line and executed-flow coverage on the same map to see what is still exposed. That end-to-end grounding is the whole point: the tests describe how your system really behaves, verified against a compiler and a test runner.
SlicerCode runs today as a VS Code extension; IntelliJ and a CLI are on the way. Download it to try test generation on your own code, browse the full capability set on the Features page, or check the Roadmap for what is coming next. If you are newer to the space, the companion guides on AI Coding Agents and Code Flow & Graph Analysis explain the foundations the generator builds on.
- Right-click a function or use the Analyzer panel → Generate tests
- Framework auto-detected; compile-run-repair drives to green before you see it
- Shares the flow graph, runtime values, and coverage with the rest of the product
- VS Code today; IntelliJ + CLI coming soon
Frequently asked questions
What is AI test generation?
AI test generation uses a language model to produce runnable test code — unit, integration, or end-to-end — from your existing source, in your project's framework and layout. The best implementations ground the model in your call graph and real runtime values so the assertions reflect actual behavior rather than guesses.
Can AI-generated tests be trusted?
They can be trusted when they are grounded and verified: assertions seeded from real captured values (not the model's guesses) and every test compiled and run before you see it. A generator that stops before executing its output is handing you a draft, not a test — SlicerCode runs a compile-run-repair loop until the suite is green.
How does AI generate unit tests?
It analyzes the target's structure and dependencies, seeds inputs and expected values from a real execution when a runtime capture exists, generates an idiomatic test in your detected framework, then compiles and runs it — repairing broken imports, mocks, and assertions until it passes.
What's the difference between line coverage and branch coverage?
Line coverage measures which source lines executed; branch coverage measures whether every conditional path was taken. A line can be 100% covered while an untested branch through it hides a bug, so line coverage is a floor, not a proof. SlicerCode reports line coverage plus executed-flow coverage — it does not claim branch coverage.
Which languages does AI test generation support?
SlicerCode generates tests across 6+ languages — Java, Kotlin, Python, JavaScript/TypeScript, Go, Ruby, and more — using tree-sitter to parse each one. It auto-detects your framework (JUnit, pytest, Jest/Vitest, go test, RSpec) and mirrors your project's conventions and layout.
Are AI-generated tests deterministic?
SlicerCode's generation is deterministic in the sense that the same source and the same runtime captures aim to produce the same verified output, rather than a different result on each run. It is also self-repairing: the compiler and test runner act as an oracle so the loop converges on tests that genuinely pass in your environment.
Keep learning
See it in your codebase
SlicerCode brings the agent, flow graphs, runtime capture, PRD validation and test generation into VS Code.