Apply to my task
arXiv:2608.17177 ★ 90 Aug 17, 2026

Spec-Driven Test Generation: contract first, tests second

Key result

You ask the LLM to 'write tests for this code' — and you often get tests that miss edge cases. The model tends to follow the code's logic instead of checking what it's supposed to do. Spec-Driven Test Generation lets you find missing edge cases through an explicit behavior contract — before you write a single test. The model spells out preconditions and postconditions, marks what's already covered (checkmark) and what isn't (X), and writes tests only for the gaps — it can't write a test 'just to have one,' only for a specific uncovered condition.

TL;DR

Spec-Driven Test Generation is a two-step technique that forces the model to first describe in words how the code is supposed to behave, and only then write tests. The model goes through each function and pins down: what's expected on input (preconditions), what's guaranteed on output (postconditions), which cases are already covered by tests, and which aren't. Only after that does it write tests to close the gaps.

When you ask an LLM agent to "write tests for this code" directly, it tends to latch onto what it sees in the code rather than what the code is supposed to do. The result, according to the authors: tests that often miss edge cases — the model has little to anchor on besides the function's literal text.

The method fixes this by turning implicit expectations into an explicit document — essentially a behavior checklist. The model can't just write a test "because why not"; it has to first explain why the test exists and which condition it's checking.


Method Blueprint

PHASE 1 (extraction, one or more prompts):
STEP 1: The model reads the code + docstrings + comments + existing tests → understands the function's business logic
STEP 2: For each function, the model spells out:
   - D (description): what the function does
   - Pre (preconditions): what must hold true BEFORE the call
   - Post (postconditions): what's guaranteed AFTER the call (success, errors, side effects)
STEP 3: The model checks each condition against existing tests → marks ✅ Tested or ❌ Untested
STEP 4: For every ❌ Untested — proposes a concrete test (Sug)
→ Output: a spec document (.spec.md)

[optional: a human edits the spec by hand before phase 2]

PHASE 2 (test generation, separate prompt):
STEP 5: The model writes tests covering all ❌ Untested conditions from the spec
→ Output: a ready-to-run test suite

Example

This example and the prompts below are our illustration. The paper does not publish its prompts, and its only worked example is a CustomerDatabase.add_customer function (invalid phone number, empty name, successful add).

Task: A developer is building the backend for an e-commerce store and wrote a function called apply_promo_code that calculates a discount from a promo code. The function is already partially covered by tests, but the developer isn't sure all the edge cases are handled (expired promo code, negative discount, double application).

Prompt (step 1):

I want to test this function. Before writing any tests, extract the contract of its behavior.

Code:
{paste the full apply_promo_code function}

Existing tests (if any):
{paste the current test file}

For the function, produce:
- Description: what it does
- Preconditions: what must be true before the call
- Postconditions: what's guaranteed after the call — including error handling, edge cases, side effects (bonus deduction, logging, etc.)
- For each condition, mark ✅ Tested (already covered by an existing test) or ❌ Untested (not covered), and propose a concrete test for each untested condition.

Don't write tests yet. Contract only.

Result: The model produces a structured list of conditions like "promo code expired → returns 'expired' error" or "discount cannot exceed order total" — each flagged as covered or not. Some conditions will come back flagged ❌ — those are the likely blind spots in the current tests.

Prompt (step 2):

Here's the contract from the previous step:
{paste the spec}

Write tests that cover every condition marked ❌ Untested. Use Arrange-Act-Assert format, with test names like "what_is_tested_under_which_condition".

Result: The model produces a ready-made set of tests, each tied to a specific missing condition — not generic "just in case" tests, but targeted checks for boundary scenarios.


Why It Works

Models are great at continuing a pattern that's been explicitly laid out, but poor at independently figuring out what's worth checking when simply asked to "write tests." Without an explicit anchor, the model tends to follow the code's structure rather than its intent — hence missed edge cases. (This is the authors' motivation; in the study the plain agent still caught 53.4% of bugs, versus 63.2% for the spec-driven agent.)

What LLMs are genuinely good at is organizing information that's already implicitly present in the text — docstrings, comments, calls to the function elsewhere in the codebase. By spelling out pre- and postconditions, the model surfaces this implicit information explicitly.

The method exploits exactly this: instead of asking the model to guess at tests right away, it first makes the model "say the contract out loud." It's like asking a lawyer to first list out every clause in a contract, and only then check whether the parties fulfilled them — instead of reading the contract and the compliance check at the same time.

Control knobs (our suggestions, not tested in the paper): - Drop the "mark Tested/Untested" step → the model just lists the contract without tying it to coverage — faster, but less targeted. - Add "include private/helper methods" → fuller coverage, but a longer spec and slower turnaround. - In step 1, add "pay special attention to error handling and boundary values" → sharpens the focus on edge cases, if that's your priority. - Add a human-in-the-loop step (you edit the spec yourself before phase 2) → more control, less model autonomy.


Prompt Template

I want to test this code. Before writing any tests, extract the behavior contract for each function.

Code:
{code}

Existing tests (if any):
{existing_tests}

For EVERY function, produce a block:

### Function: {name}
- Description: what the function does and what it's for
- Preconditions: what must be true BEFORE the call
- Postconditions: what's guaranteed AFTER the call — error handling, side effects, return values, edge cases
- For each pre- and postcondition, mark:
  ✅ Tested — if the condition is already checked by an existing test
  ❌ Untested — if the condition isn't covered, and propose a concrete test

Don't write tests yet. Contract only.

---
(After receiving the contract — second prompt)

Here's the contract above. Write tests that cover every ❌ Untested condition. Use Arrange-Act-Assert format, with clear test names.

Drop your code into {code}, and existing tests (if any) into {existing_tests}.

🚀 Quick Start — paste this into the chat:

Here's the Spec-Driven Test Generation template. Adapt it to my task: {your task — e.g., "test the discount calculation function in an e-commerce store"}.
Ask me questions to fill in the fields.

[paste the template above]

The LLM will ask about the code, programming language, and whether tests already exist — because without that it can't figure out what's covered and what isn't. It'll take the pattern from the template and adapt the contract structure to your specific code.


Limitations

⚠️ Scope of applicability: the method was tested on 90 Google production bugs whose fixes touched a single file, with one model family (Gemini 3 Flash) writing the tests. The authors note results may differ on other codebases and other models. For UI code, one-off scripts, or code without a clear behavioral contract, the payoff likely won't be as strong (our guess, not tested).

⚠️ Cost: two steps instead of one means more tokens and more generation time — in the study about 38% more tokens overall. For small, trivial functions, the overhead might not be worth it.

⚠️ Dependent on context quality: if the code has no docstrings, comments, or existing tests, the model has less to anchor on when deriving the contract — spec quality may suffer (our reasoning, not measured).

⚠️ Not a silver bullet against misses: even with the method, some contracts remain incomplete (per the study, the contract described the behavior behind the bug in 61% of bugs with one run and 79% with five runs — not 100%).


How It Was Studied

The researchers took 90 real bugs from Google's internal issue tracker (C++, Java, Python, Go) — not synthetic ones, but actual production bugs with confirmed fixes. They compared two agents running on the same model (Gemini 3 Flash) with the same toolset: a plain agent that's simply asked to "write tests," and a spec-driven agent that's required to write a contract first.

Tests were run in a "clean slate" setting — existing tests were removed and the agent did not see the bug report or the fix, so it had to rely on understanding the code. The optional human editing step was skipped. Each configuration was run 5 times per bug, with statistics computed via bootstrap and McNemar's test.

Result: counting a bug as caught if any of the 5 runs caught it, the spec-driven agent caught 9.8 percentage points more historical bugs (63.2% vs 53.4%) and achieved 2.5 points higher branch coverage (48.9% vs 46.4%) — both differences statistically significant. With a single run the gap was only 4.2 points and not significant, and line coverage did not change. A separate, stronger judge model (Gemini 3.1 Pro) rated the spec-driven tests better overall than the baseline in 77.8% of cases, and better than the developers' own tests in 56.7% of cases — the authors call this on par or marginally better. An interesting detail: the more precisely the contract "caught" the actual root cause of the bug, the higher the chance the resulting test would find that bug (54.9% vs. 19.4% when the contract didn't match the bug) — meaning the quality of this intermediate step directly predicts the final outcome.


Adaptations and Extrapolations

🔧 Technique: carrying the principle over from tests to any code review

Our extrapolation, not tested in the paper: the same trick may work beyond tests. Before asking a model to review or rewrite code, have it first write out the contract: what the function is supposed to guarantee. The model may start spotting mismatches between what the code promises and what it actually does — and that's exactly where bugs live.

Before reviewing this code, write out for each function: what it should guarantee on input and output (based on docstrings, naming, and how it's used elsewhere). Then check the code against this contract and point out where the implementation doesn't match the expected behavior.

🔧 Technique: contract as documentation

The spec itself (Phase 1) is a useful byproduct (our suggestion). Even if you don't need tests, ask the model to generate this kind of contract for undocumented legacy code — you'll get a readable description of the system's behavior that you can hand to a coworker instead of spending an hour explaining "how this thing works."


Resources

Tufano M., McClure J., Cambronero J., Cheng R., Shi S.Y., Wei R., Chen D., Ivancic F., Dalloro L., Rondon P. Grounding AI Agents in Contracts: An Empirical Evaluation of Spec-Driven Test Generation. Google, 2026.

Research digest

Key result

You ask the LLM to 'write tests for this code' — and you often get tests that miss edge cases. The model tends to follow the code's logic instead of checking what it's supposed to do. Spec-Driven Test Generation lets you find missing edge cases through an explicit behavior contract — before you write a single test. The model spells out preconditions and postconditions, marks what's already covered (checkmark) and what isn't (X), and writes tests only for the gaps — it can't write a test 'just to have one,' only for a specific uncovered condition.

Principle

It's like asking a lawyer to first list out all the clauses of a contract, and only then check whether they've been fulfilled — instead of reading the contract and the tests at the same time. The model states what the code SHOULD do, not what it ALREADY does — and only after that does it sit down to write tests.

Why it works

LLMs are great at breaking down what's already hidden in docstrings and comments. But they're bad at figuring out on their own what's worth checking if you just ask 'write tests.' Without a reference point, the model copies the code's structure instead of its meaning. The contract drags the implicit stuff out into the open — and the model can no longer ignore it. That said, even with this approach, the contract described the behavior behind the bug in only 61% of bugs with one run and 79% with five runs — so it's not a full safety net. In the study (Google code, Gemini 3 Flash) bug detection rose from 53.4% to 63.2% across five runs.

When to apply

Backend code with clear inputs and outputs → functions, methods, APIs, especially when you need to find missing edge cases in old code without solid test coverage. NOT suited for UI code and one-off scripts without a clear behavior contract — there's nothing for the model to latch onto there.

Mini recipe

1. Gather context: the function's code + docstrings + comments + existing tests (if any) 2. Ask for a contract, not tests: preconditions (what's true before the call), postconditions (what's guaranteed after — including errors and side effects) 3. Force it to mark coverage: for each condition — already tested or not, and a specific test suggestion for the uncovered ones 4. Eyeball it: the contract should be easy to read, easy to spot if the model missed an important case — fix it by hand 5. Order the tests in a separate request: only for the uncovered conditions, in Arrange-Act-Assert format

Examples

Our illustration (the paper publishes no prompts): [BAD]: `Write tests for the apply_promo_code function` [GOOD]: `Before writing tests, extract the contract: description, preconditions, postconditions. For each one, mark whether it's covered by an existing test or not, and suggest a test for anything uncovered. Don't write tests, just the contract.`

In plain words

Models often write shallow tests if you just hand them code and say: "make sure it works." They tend to follow the function's structure instead of its intended behavior. The result is tests that miss edge cases — and so miss bugs. To get a model to actually hunt for bugs, you need to ground it with Spec-Driven Test Generation — a two-step method where the AI first spells out a behavior contract in words, and only then generates the actual test code.

It's like hiring a security guard and telling him: "keep an eye on things," without giving him a list of rules. He'll just stand there and wave any thief right through, because nobody told him what's actually off-limits. But hand him a clear job description spelling out every "you can't do that," and he'll nail the violator instantly. Without an explicit contract, a model's tests give weaker protection than they could.

The method forces the AI to work systematically: first comes pinning down preconditions (what goes in) and defining postconditions (what's guaranteed to come out). The model describes in plain language "what the function actually does," compares that against the existing test suite, and runs a gap-scan for blind spots. That's how it catches forgotten edge cases — in a shop, say (our example), a negative discount, an empty cart, or an expired promo code — and only then, as step two, writes tests for them.

The approach was tested on 90 real bugs from Google's own code (C++, Java, Python, Go): the spec-first agent caught about 10 percentage points more of them (63% vs 53%, best of five runs). It was tested with one model family, so results may differ elsewhere. It is worth trying anywhere a bug costs real money: fintech and billing, microservices, complex auth systems. Where a plain prompt gives you only a sense of reliability, the contract-based approach pushes the model toward checking the business logic.

Bottom line: stop asking the model to "write some tests" in one shot — you'll get shallower checks. Make it pin down the function's contract first and hunt for gaps in the scenarios. Spec before code is a simple way to push a model toward being a nitpicky QA engineer who hunts for real bugs — not a guarantee, since the contract still missed the bug's behavior in about a fifth of cases.

AI lab assistant

Apply this study to your task

Describe what you're working on. AI takes the method and the measured results from this paper and adapts them to your case, or writes a ready prompt built on them.

Based on Spec-Driven Test Generation: contract first, tests second arXiv:2608.17177

Sign in to apply this study New accounts get 50 free credits.