Contents
Agentic coding and test-driven development (TDD) fit together exceptionally well, because tests give a coding agent exactly what it otherwise lacks: an objective, machine-readable criterion for whether its work is complete and correct. Defining tests up front lets the agent work towards a clear target instead of a vague description.
Why tests are so valuable for coding agents
A coding agent works in a loop of planning, executing and verifying. The quality of the verification determines how well it can correct itself. Without tests, it only has the compiler – and the compiler says nothing about whether a discount calculation is correct from a business perspective or whether a booking system detects overlapping appointments.
In agentic coding, tests play several roles at once:
- Specification: they describe precisely which behaviour is expected, including edge cases.
- Feedback: failing tests produce concrete error messages the agent can work with.
- Acceptance criterion: "all tests pass" is an unambiguous end to the task – instead of a confident but unverified "done".
- Regression protection: existing tests stop the agent from silently breaking something else while solving the task.
The classic TDD cycle with an agent
The familiar red-green-refactor cycle transfers directly to working with agents, with one important addition: a human reviews the tests before implementation starts.
- Describe the requirement: state the desired behaviour with examples, inputs and expected outputs.
- Have the agent write tests: explicitly without implementation. It runs them and confirms they fail (red).
- Review the tests: check whether they describe the right behaviour and cover relevant edge cases. This is the single most important step of the whole process.
- Commit the tests: a separate commit makes any later change to the tests immediately visible in the diff.
- Have the agent implement: it writes code until all tests pass (green) – with the clear instruction not to modify the tests.
- Refactor: with green tests as a safety net, the agent can clean up the code; tests run again after every step.
Common pitfalls and how to avoid them
TDD with agents is effective, but not foolproof. The most frequent problems in practice:
| Pitfall | What happens | Countermeasure |
|---|---|---|
| Tests get adjusted | The agent changes expected values so tests pass | Commit tests separately, mark test files as read-only in the task, check the diff |
| Hard-coded solutions | Code recognises test inputs and returns matching values instead of implementing the logic | Multiple varied test cases, property-based tests, code review |
| Trivial tests | Tests only check that a function exists or does not throw | Specify concrete expected values and edge cases in the task |
| Excessive mocking | Tests only check how mocks interact, not real behaviour | Define mocking rules in the context file, add integration tests |
| Disabled tests | Difficult tests are skipped or commented out | Configure CI so that skipped tests are flagged |
Our article on ensuring the quality of AI-generated code describes further ways to safeguard AI output.
Which test types suit which tasks
- Unit tests: ideal for business logic, calculations, validation and data transformation. Fast and precise, perfect for short feedback loops.
- Integration tests: useful for API endpoints, database access and the interplay of several modules. Somewhat slower, but closer to reality.
- End-to-end tests: check complete user flows such as sign-up or checkout. Valuable for agents, but slow – better as a final check than in the inner loop.
- Snapshot and visual tests: helpful for UI components, but they require particularly careful review because agents can easily just update snapshots.
Our article on test automation with AI goes deeper into how agents help write these tests.
TDD for existing code without tests
Many projects have areas without adequate tests. A two-stage approach has proven effective here: first, have the agent write so-called characterisation tests that capture current behaviour – even if that behaviour is buggy. These tests form the safety net. Only then do you make changes following the TDD cycle. This prevents the agent from silently changing behaviour during refactoring that other parts of the system rely on.
Checklist for TDD with coding agents
- The test command is documented in the context file and runs in seconds, not minutes.
- The task contains concrete examples with expected results and edge cases.
- Tests are written before implementation, executed and reviewed by a human.
- Tests and implementation live in separate commits.
- The agent may not modify test files during implementation.
- The full test suite runs before the pull request is opened.
- Reviewers look at test changes especially critically.
TDD is also one of the core workflows we describe in agentic coding workflows for web and app projects. For teams that want to practise the approach hands-on, we place AI developers from real-world practice as trainers – workshops from €799 net, content by arrangement.
Planning an app? Request a free fixed-price quote in 2 minutesConclusion
Test-driven development makes coding agents more reliable because it turns a vague instruction into a verifiable goal. The human's focus shifts from writing the implementation to carefully defining and reviewing the tests. Teams that know the typical pitfalls and consistently check test changes get a pairing that delivers faster and more safely than either approach on its own.
Frequently Asked Questions
Should the agent write the tests itself?
Yes, that saves time, but a human must review the tests before implementation. The tests are the specification, and a flawed specification leads to flawed code.
How do I stop the agent from manipulating tests?
Commit tests separately, explicitly forbid changes to test files in the task and specifically review the diff of test files.
Does TDD with agents work for front-end code?
Yes, especially for component logic, form validation and state management. For visual appearance, visual checks and manual review complement the tests.
Is TDD worthwhile for small bug fixes too?
Especially there: a test that reproduces the bug proves the fix works and prevents the bug from returning later.