Back to Blog
ai-agents
3 min read
AI-powered content

Test-Driven AI Coding: Automating the Code-Test-Fix Loop in Sandboxes

Learn how Google Antigravity 2.0 uses closed-loop TDD to automate writing unit tests, running them in sandboxes, and self-correcting buggy code.

tdd, google-antigravity, automation

The biggest issue with raw AI-generated code is functional verification. An LLM can write syntactically correct code that compiles perfectly, yet fails to solve the actual business logic or breaks edge cases in production.

To bridge this gap, senior engineers rely on Test-Driven Development (TDD).

In Google Antigravity 2.0, the agent uses TDD as its primary code quality safeguard. By automating the write-test-run-correct loop inside its isolated sandbox, it guarantees that code meets functional specifications before asking for your merge approval.


The TDD Agent Loop

Instead of writing implementation code first and hoping for the best, Antigravity follows a structured four-stage validation loop:

[1. Write Tests] -> [2. Run & Fail (Red)] -> [3. Write Implementation] -> [4. Run & Pass (Green)]
        ^                                                                          |
        |------------------- (If Test Fails due to bugs) --------------------------|

Step 1: Writing the Specs (The Test Cases)

When assigned a task (e.g., "Add an email validator with domain whitelist support"), Antigravity begins by looking at the codebase's existing testing framework (Jest, Vitest, Pytest, Go testing). It writes a new test suite that asserts:

  • Happy paths (valid inputs).
  • Boundary conditions (empty strings, oversized payloads).
  • Error paths (invalid domains, SQL injection attempts).

Step 2: The Red Light (Initial Execution)

The agent runs the testing tool inside its sandbox. Since the feature code doesn't exist yet, the test run fails, spitting out assertion errors.

  • Why this matters: It verifies that the testing harness is correctly configured and that the test actually catches the missing implementation.

Step 3: Implement & Self-Correct (The Green Light)

Now, the agent writes the implementation code to satisfy the tests. It runs the test suite again. If the tests fail, the Self-Correction Engine takes over:

  1. Stack-Trace Parsing: The agent reads the test output stream (including stdout, stderr, and line-by-line assertion diffs).
  2. Context Synthesis: It matches the exact line of the failing test against its implemented code logic.
  3. Surgical Tweaking: It makes precise adjustments to fix the bug, then re-runs the tests.

This cycle repeats automatically in the background until the tests report 100% green status.


Real-world Example: Node.js API Verification

Here is how Antigravity handles a failing test case in its terminal logs:

[Sandbox Command] npm run test
-> FAIL  tests/validator.test.ts
   ● Email Validator › should reject domains not in whitelist
     expect(received).toBe(expected) // Object.is equality
     Expected: false
     Received: true
     at Object.<anonymous> (tests/validator.test.ts:18:14)

[Agent Reflection] 
"The domain validation check failed to reject 'malicious.com' because my regex matched subdomains too loosely. I will edit lib/validator.ts line 14 to restrict matching to exact domain endings."

[Sandbox Command] git diff lib/validator.ts
[Sandbox Command] npm run test
-> PASS  tests/validator.test.ts (12ms)

Why Sandboxed TDD is the Future of Coding

By shifting verification from human code reviews to automated sandboxed test execution:

  • Zero Regressions: You can rest assured that new feature additions won't break existing codebase systems.
  • Higher Autonomy: The agent can resolve its own coding bugs without interrupting you for clarification.
  • Living Documentation: Every feature implemented by the agent comes pre-bundled with rich, functional unit tests.

On Day 6, we'll learn how to customize and control this agent's coding style and validation parameters using custom project rules.

tddgoogle-antigravityautomationtesting

Related Content

Articles

Related Tools

Related Workflows