Workflows & Automation

Use Test-Driven Development with AI agents by designing the tests first and letting the agent implement the solution.

Write Python tests first, then let AI agents implement code until pytest passes, keeping correctness under developer control.

Key Takeaways

  • The article teaches test-driven development with AI agents by having humans design the tests first, then letting the agent write implementation code until the test suite passes.
  • The workflow keeps correctness under human control: you define the behavior contract, author and harden tests, and block the agent from editing tests during implementation.
  • A five-step loop is used: set the contract and first test, let the agent draft more tests, review and harden them, let the agent implement, then inspect the code diff.
  • The practice project is a Python version comparator for strings like 1.0.dev1, 1.0a1, and 1.0, using pytest and a separate tests/ directory.
  • The method is not tied to one AI tool; it can work with Claude Code, GitHub Copilot CLI, OpenCode, or similar repository-aware agents.

Meta description: Use test-driven development with AI agents: write Python tests first, then let Claude Code or another agent implement a version comparator using pytest.

Tags: test-driven development, AI coding agents, pytest, Python version comparator

Featured image: abstract friendly illustration of tests checking code written by an AI agent, no real people, logos, or text.

Real Python's tutorial on test-driven development with AI agents is for Python developers who want an agent to write implementation code without giving the agent control over correctness. The workflow keeps the design in your hands: you author the tests that state what a feature should do, then the agent produces code until those tests pass. A passing suite proves the implementation fits the tests. If the same AI agent creates both sides, the result only shows internal agreement.

The practice target is a small Python version comparator. The version specifiers specification defines Python ordering: 1.0.dev1 comes before 1.0a1, and 1.0a1 comes before 1.0. The packaging library would normally handle this in production, but the tutorial builds a comparator from scratch so you can test against a real contract.

The loop keeps the old discipline

The test-driven development cycle remains familiar: create a test that fails, get it passing, then clean up the code. An AI agent changes the division of labor, not the method. You own the design choices; the agent handles drafting.

Each turn has five steps. You complete three, and the agent completes two.

  1. Set the contract and author the first test. Decide the external behavior of the feature, then write the first test yourself.
  2. Let the agent draft more tests. The agent uses only your contract and first test to generate additional cases in the same style.
  3. Review and harden the tests. Compare the generated tests with your contract and add any missing edge cases.
  4. Let the agent implement. The agent writes code until the suite passes while you block writes to tests/.
  5. Review the diff. Inspect the code changes, then move to the next feature.

The second step is the key difference from handing a feature to an agent. The agent creates tests before implementation exists. If you request tests after the code is already written, the tests will describe that code, bugs included.

Step 1: lock the behavior into a test

Before the agent writes anything, you decide what the feature does and capture that decision in a test. Later agent output has to fit the shape you set.

The feature is a Version class that turns a version string into an object you can compare with < and >. The project uses a flat layout. Implementation goes in core.py, and tests go in a separate tests/ directory. The sample files are version_check/__init__.py, version_check/core.py, and tests/test_core.py. Create these files first, including an empty core.py for the agent to fill later. Then export the public Version name from __init__.py.

The separation matters because the tests are your contract. The implementation is the agent's draft. If the tests remain outside the agent's write path during implementation, the code has to satisfy the tests instead of the tests being shaped around the code.

What you need before starting

You need Python 3.11 or newer along with pytest. You also need 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 to install. Create and activate a virtual environment, then install pytest there.

You should know how to use pytest, especially how to parametrize tests. Prior experience with test-driven development helps, but it is not required. Real Python's Test-Driven Development With pytest video course covers writing unit tests, running them, and catching bugs before users encounter them. The tutorial walks through the cycle step by step.

The workflow does not depend on one agent

The demonstration uses Claude Code, but the method is not tied to it. Repository-aware agents such as GitHub Copilot CLI, Antigravity CLI, and OpenCode can follow the same workflow. Real Python's AI coding agents guide explains how IDE, terminal, pull request, and cloud agents fit into a Python workflow.

Related resources in the source

The tutorial provides a free project and test suite for building the Python version comparator by designing tests first and letting an AI agent write the implementation. It also includes an interactive quiz named TDD With AI Agents: Design the Tests First, Let the Agent Implement. You receive a score upon completion to help track your learning progress.

For a larger practice project, Real Python's Building Python Projects With AI Agents live course has you build a real project while an agent writes every line. You write the specification, judge what comes back, and sharpen your testing judgment. The course has a waitlist for the next dates. Real Python also sends a short Python Trick by email every couple of days through Python Tricks. The full article is at https://realpython.com/tdd-ai-agents/ ».

Common mistakes

  • Letting one AI agent write both the tests and the implementation, then treating a green suite as proof of correctness.
  • Asking for tests after the code already exists, which makes the tests describe the implementation, bugs included.
  • Skipping the review and hardening step, leaving missing edge cases outside the contract.
  • Forgetting to block writes to tests/ while the agent implements the code.
  • Assuming the workflow requires one specific agent. It can work with Claude Code, GitHub Copilot CLI, Antigravity CLI, or OpenCode.

Comments (0)

  1. No comments yet. Be the first to share what worked for you.

Leave a comment

Comments are reviewed before they appear. Your email address is not published.