# Test-Driven Development

> Practice the red, green, refactor cycle to drive design with tests. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/test-driven-development
Last updated: 2026-11-28

Practice the red, green, refactor cycle to drive design with tests.

Intermediate · about 4 hours · difficulty 3/5

Learn first: [Unit Testing Fundamentals](https://www.internhack.in/roadmaps/tracks/backend/unit-testing)

**Test-Driven Development (TDD)** flips the usual order: write a failing test first, write the minimum code to make it pass, then refactor with the test as a safety net. The cycle is short, often just a few minutes: **red, green, refactor**.

### Why people practice it

- It forces you to think about the interface and behavior before implementation.
- It produces code that is testable by construction, with small units and clear dependencies.
- Refactoring becomes fearless because every behavior is pinned by a test.

### How to practice

- Start with **katas** (small exercises such as a bowling score calculator or a string calculator) to build the rhythm.
- Take tiny steps. If a test takes more than a few minutes to make green, the step was too big.
- Refactor both production code and tests; duplication in tests hurts too.

### Where it fits best

TDD shines for business logic with clear rules (pricing, validation, state machines) and for bug fixes: write a test that reproduces the bug before fixing it. It is less natural for exploratory spikes or UI layout. Many experienced developers use it selectively rather than dogmatically.

You are ready to move on when you have completed a kata in strict TDD and fixed at least one real bug by writing the failing test first.

## Key concepts

- **Red, green, refactor**
- **Coding katas**
- **Test-first bug fixing**
- **Behavior-driven development (BDD)**

## Mini project

Build a shopping cart module with discounts and tax rules entirely test-first, committing after each green step so the history shows the cycle.

## Check yourself

Why does TDD tend to produce code with fewer hidden dependencies?

## Free resources

- [Test Driven Development](https://martinfowler.com/bliki/TestDrivenDevelopment.html) (Martin Fowler)
- [Learn Go with Tests](https://quii.gitbook.io/learn-go-with-tests) (Chris James)

Previous: [Integration and End-to-End Testing](https://www.internhack.in/roadmaps/tracks/backend/integration-e2e-testing) · Next: [Continuous Integration and Delivery](https://www.internhack.in/roadmaps/tracks/backend/ci-cd)

## More in Testing and Shipping Continuously

- [Unit Testing Fundamentals](https://www.internhack.in/roadmaps/tracks/backend/unit-testing)
- [Integration and End-to-End Testing](https://www.internhack.in/roadmaps/tracks/backend/integration-e2e-testing)
- [Test-Driven Development](https://www.internhack.in/roadmaps/tracks/backend/test-driven-development)
- [Continuous Integration and Delivery](https://www.internhack.in/roadmaps/tracks/backend/ci-cd)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
