Free · Daily brief + weekly playbookDaily 60-sec AI Voice Brief + Weekly Production AI Agent PlaybookGet the brief →
ReleaseInsight · On RadarHabr AI→ original

Tests Instead of a Prompt: How TDD Makes an AI Agent Write Code to Spec

TDD, or test-driven development, means writing tests first and then the code that passes them. Egor Gurin of MTS Web Services suggests working the same way with an AI agent: eight tests for phone-number search replaced a long prompt, and the agent wrote the required function on its own. The test is both the task and the acceptance check, so code without green tests should not be accepted.

AI-processed from Habr AI; edited by Hamidun News
Tests Instead of a Prompt: How TDD Makes an AI Agent Write Code to Spec
Source: Habr AI. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceHabr AI ↗
Editor-in-Chief
Red-Green-Refactortests fail, then pass, then the code gets cleaned up
Order rulePay() is allowed only from the Created status
Phonethe number gets the prefixes +7, 7, 8, 08, and a variant without any prefix

When you work with an AI agent, you quickly run into one problem: the model produces a lot of code, and you have to check it by hand. Egor Gurin, a Go developer at MTS Web Services, proposed on Habr using TDD (Test Driven Development): first write tests that describe the required behavior, and only then ask the agent to write code that passes those tests. Instead of a long text prompt, the agent gets ready-made examples of the form "this input, that output." This is a follow-up to his earlier article on Document Driven Development, where contracts between server and client are written before development begins.

The test as both the task and the acceptance check

Classic TDD follows a traffic-light cycle: red, when the tests fail because the code isn't written yet; green, when they pass; refactor, when the code gets tidied up. Gurin carries this cycle over to working with LLMs. First, a person describes the method signatures and writes the tests. Then the agent is asked to write an implementation that makes the tests pass.

According to the author, people who use LLMs fall into two camps. Some hand off responsibility entirely to the model and barely check the result. Others split the task into small steps and guide the agent by hand. TDD helps both: AI follows clear rules and patterns that already exist in the project well. And if the desired behavior is hard to put into words, it can be shown through expected outputs. The test then plays two roles: it is both the prompt and the acceptance condition. While the tests are red, the task is not considered done.

Two examples from practice

The first example is simple: a business rule for an order. An order can move to the Paid status only from the Created status. The author first declares a Pay() method with a "Not Implemented" stub. Then he writes a table-driven test with three cases: paying a created order succeeds, while paying an already paid or canceled order returns an invalid-transition error. When the tests run, they fail. The agent writes a short implementation with a status check, and the tests turn green.

The second example comes from his work. A user searches for operator-to-customer interactions by phone number. The number can be entered as +7 (900) 123-45-67, as 79001234567, 89001234567, 089001234567, or simply 9001234567. Numbers are also stored in different formats in the database. So the entered number has to be expanded with all the prefix variants, and the search runs against each one. At the same time, short service numbers like 12345 and numbers of other formats must not be touched.

Here the tests describe the task better than a paragraph of text would. The author wrote eight cases: five formats of an ordinary number, a service number, a non-phone-type number, and nil as input. From these the agent generated a regular expression that extracts the ten digits of the number, and a function that adds the prefixes +7, 7, 8, and 08, along with the variant without a prefix. The tests passed, the author was satisfied with the code, and no refactoring was needed.

What this gives in practice

Gurin sees TDD as a way to keep the role of designer for himself while leaving the LLM as an executor with a clear assignment. A bonus: the output comes with proper test coverage, especially in the application layer, where the product's business rules live.

For teams that already use agents, this leads to a few simple steps:

  • write the tests and signatures yourself, and hand code generation to the agent;
  • include edge cases in the tests: empty input, service values, unexpected formats;
  • run the tests after every generation and don't accept the code until everything is green;
  • leave refactoring to a human if the implementation formally passes but reads poorly.

Limitations

The material is the author's personal experience, with no measurements. The text gives no data on how much time is saved, how often the agent passes the tests on the first try, or how it handles larger tasks. Both examples are small: a pure function and a single entity method. On a complex integration with a database, queues, and external services, writing tests is harder, and green tests don't guarantee the code is good. If you forget to describe some case, the agent won't account for it either, so responsibility for the completeness of the tests stays with the human.

Still, the idea works and costs little: you had to write tests anyway. Now they also explain to the agent what is expected of it more precisely than any prompt would.

Story

Key Questions Answered

What are the key details on The test as both the task and the acceptance check?

Classic TDD follows a traffic-light cycle: red, when the tests fail because the code isn't written yet; green, when they pass; refactor, when the code gets tidied up. Gurin carries this cycle over to working with LLMs. First, a person describes the method signatures and writes the tests. Then the agent is asked to write an implementation that makes the tests pass. According to the author, people who use LLMs fall into two camps. Some hand off responsibility entirely to the model and barely check the result.

What are the key details on Two examples from practice?

The first example is simple: a business rule for an order. An order can move to the Paid status only from the Created status. The author first declares a Pay() method with a "Not Implemented" stub. Then he writes a table-driven test with three cases: paying a created order succeeds, while paying an already paid or canceled order returns an invalid-transition error. When the tests run, they fail. The agent writes a short implementation with a status check, and the tests turn green.

What this gives in practice?

Gurin sees TDD as a way to keep the role of designer for himself while leaving the LLM as an executor with a clear assignment. A bonus: the output comes with proper test coverage, especially in the application layer, where the product's business rules live.

ZK
Zhemal Khamidun — Head of AI at Alpina Digital · MIPT (academic supervisor of the Tech Entrepreneurship department). Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
Jemal Hamidun

Editor-in-chief · Cite this analysis

Jemal Hamidun

Head of AI at Alpina Digital · Academic Supervisor of the Tech Entrepreneurship Department at MIPT

Head of AI & CPO of AlpinaGPT at Alpina Digital, Academic Lead of Tech Entrepreneurship at MIPT. Founder of Hamidun News and Vibe Academy.

Ready-to-paste citation for journalists, blogs and Telegram channels

Source: Hamidun News — Tests Instead of a Prompt: How TDD Makes an AI Agent Write Code to Spec https://news.hamidun.com/en/news/17211/tests-instead-of-a-prompt-how-tdd-makes-an-ai-agent-write-co

What do you think?
Loading comments…