A person holding checklists beside a cabinet of locked tests labeled TESTS, with a door marked BLOCKED separating it from a robotic arm and machinery.

TDD With AI Agents: Design the Tests First, Let the Agent Implement

When your test suite passes, you know the code matches your tests. However, if an AI agent wrote both, then all you know is that the agent agrees with itself. TDD with AI agents keeps you in control. First, you design the tests that define what the feature should do, and then the agent writes code to make those tests pass.

Test-driven development (TDD) is a cycle in which you write a failing test, make it pass, and then refactor. Working with an AI agent doesn’t change this cycle, but it changes who handles which part. You make the design decisions, and the agent does the drafting:

Loop diagram with five steps showing your steps (define the contract, harden the tests, review the diff) and the agent's steps (draft tests, implement), looping back for the next feature.
TDD With AI Agents Loop

This loop is how you apply TDD when you let an AI agent write the implementation.

In this tutorial, you’ll practice this loop by building a small Python version comparator. Python has its own versioning rules, defined in the version specifiers specification, where 1.0.dev1 sorts before 1.0a1, which sorts before 1.0. While you’d typically use the packaging library in production, you’ll build your own here to work with a real contract worth testing.

Take the Quiz: Test your knowledge with our interactive “TDD With AI Agents: Design the Tests First, Let the Agent Implement” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

TDD With AI Agents: Design the Tests First, Let the Agent Implement

Check your grasp of TDD with AI agents in Python. Write the contract and tests first, then let a coding agent implement code that passes them.

Prerequisites

To follow along with this tutorial, you’ll need a few tools on your machine:

  • Python 3.11 or newer along with pytest
  • A coding agent that can read and write files in your project

The sample project imports nothing beyond Python’s standard library, so pytest is the only package that you’ll need to install. Create and activate a virtual environment first, and then install pytest into it.

You should be comfortable with pytest, including how to parametrize tests. Prior experience with test-driven development helps too, but it isn’t required. If you want some practice first, Real Python’s Test-Driven Development With pytest video course shows you how to write unit tests, run them, and catch bugs before your users do. This tutorial walks you through the cycle one step at a time.

The demo uses Claude Code, but the loop doesn’t depend on it. Other repository-aware agents like GitHub Copilot CLI, Antigravity CLI, and OpenCode can follow the same workflow. If you haven’t worked with one before, then Real Python’s AI coding agents guide explains how IDE, terminal, pull request, and cloud agents each fit into a Python workflow.

The Loop for TDD With AI Agents

Each turn of the loop has five steps. You handle three of them and let the agent handle two:

  1. Define the Contract and Write the First Test: You decide how the feature should behave externally and write the first test by hand.
  2. Let the Agent Draft the Tests: The agent reads your contract and your first test, then generates more test cases in the same style.
  3. Review and Harden the Tests: You check the generated tests against your contract and add any missing edge cases.
  4. Let the Agent Implement: The agent writes code until the tests pass, while you block writes to tests/.
  5. Review the Diff: You check the code changes and then move to the next feature.

Step 2 is where this loop differs from handing over the whole feature to an agent. The agent drafts tests while reading only your contract and your first test. If you ask for tests after the implementation code already exists, then the tests will describe that code, bugs and all.

Step 1: Define the Contract and Write the First Test

The first step is yours. Before an agent writes anything, you decide what the feature does and lock that decision into a test. Everything the agent produces later has to fit the shape you set here.

The feature that you’ll build to practice the TDD workflow is a Version class, which turns a version string into something you can compare with < and >. You’ll first lay out the project, then define what Version promises and write the test that pins it down.

Set Up the Project

You’ll build a small project with a flat layout. Your implementation will live in core.py, and your tests will sit in a separate tests/ directory:

version-check/
│
├── version_check/
│   ├── __init__.py
│   └── core.py
│
└── tests/
    └── test_core.py

Create these directories and files now, including an empty core.py that the agent will fill in later. Then export the public Version name from __init__.py:

Language: Python Filename: version_check/__init__.py
from version_check.core import Version

__all__ = ["Version"]

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Yuka Tamura

Yuka is a software engineer with a diverse full-stack background.

» More about Yuka

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: intermediate ai testing