# TDD, BDD, and ATDD: What They Are, How They Differ, and When to Use Each

Testing methodologies are one of those topics where everyone has an opinion but few have clarity. TDD, BDD, and ATDD are often conflated, misunderstood, or dismissed as "the same thing with different names."

They're not. Each solves a different problem, involves different people, and produces different results.

## TDD — Test Driven Development

**Who:** Developers.
**Focus:** Code correctness at the unit level.

TDD follows a strict cycle:

1. **Red** — Write a test for functionality that doesn't exist yet. It fails.
2. **Green** — Write the minimum code to make the test pass.
3. **Refactor** — Clean up the code while keeping tests green.

```js
// Red: test first
test('add returns sum of two numbers', () => {
  expect(add(2, 3)).toBe(5);
});

// Green: minimal implementation
function add(a, b) {
  return a + b;
}

// Refactor: nothing to refactor here — it's already clean
```

### Why TDD Works

- **Forces design thinking.** Writing the test first means you think about the interface before the implementation.
- **Catches regressions immediately.** Every change runs against the full test suite.
- **Produces testable code.** Code written to pass tests is inherently modular and decoupled.

### The Common Mistake

Most teams that "do TDD" actually write tests after the code. That's not TDD — that's just testing. The order matters. Writing the test first changes how you think about the problem.

## BDD — Behavior Driven Development

**Who:** Developers, QA, product managers — together.
**Focus:** System behavior described in human-readable language.

BDD extends TDD by expressing tests in natural language using the **Given-When-Then** format:

```gherkin
Feature: User login

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When they enter valid credentials
    Then they should be redirected to the dashboard
    And they should see a welcome message

  Scenario: Failed login with wrong password
    Given the user is on the login page
    When they enter an invalid password
    Then they should see an error message
    And they should remain on the login page
```

### Why BDD Works

- **Shared language.** Non-technical stakeholders can read and validate the specifications.
- **Living documentation.** The tests *are* the documentation. When they pass, the documentation is accurate.
- **Catches misunderstandings early.** When the product manager reads the scenario and says "that's not what I meant," you've saved a sprint of wasted work.

### Tools

- **Cucumber** (JavaScript, Ruby, Java) — the most popular BDD framework
- **Jest + Gherkin** — for JavaScript-native BDD
- **Behave** (Python)

## ATDD — Acceptance Test Driven Development

**Who:** The entire team — developers, QA, product, and stakeholders.
**Focus:** Acceptance criteria defined before development begins.

ATDD is similar to BDD in structure, but the emphasis is different. While BDD focuses on behavior specification, ATDD focuses on **acceptance criteria** — the conditions that must be met for a feature to be considered done.

The process:

1. **Discuss** — The team defines acceptance criteria collaboratively.
2. **Distill** — Criteria are written as executable tests.
3. **Develop** — Code is written to satisfy the acceptance tests.
4. **Demo** — The passing tests demonstrate that the feature meets requirements.

### The Key Difference from BDD

BDD is bottom-up: behaviors define the system.
ATDD is top-down: business requirements define the acceptance criteria.

In practice, many teams blend both — using BDD-style syntax (Given-When-Then) to express ATDD acceptance criteria.

## Comparison

| Aspect | TDD | BDD | ATDD |
|---|---|---|---|
| Primary audience | Developers | Dev + QA + Product | Entire team + stakeholders |
| Test level | Unit | Behavior / Integration | Acceptance |
| Language | Code | Natural language (Gherkin) | Natural language |
| Focus | Code correctness | System behavior | Business requirements |
| Written by | Developers | Developers + QA | Team collaboratively |
| When tests are written | Before each unit | Before feature development | Before sprint/iteration |

## Which Should You Use?

**Use TDD when:**
- You're writing library code, utilities, or algorithms
- You want confidence that individual functions work correctly
- You're refactoring existing code

**Use BDD when:**
- You need shared understanding between technical and non-technical team members
- You want tests that serve as living documentation
- Your application has complex user-facing behaviors

**Use ATDD when:**
- You need stakeholder sign-off before development
- You're working in regulated industries where acceptance criteria must be documented
- You want to prevent scope creep by locking down acceptance criteria early

**Use all three when:**
- You're building a complex system where unit correctness (TDD), behavior specification (BDD), and business acceptance (ATDD) all matter

They're not competing methodologies. They're complementary layers.

## The Bottom Line

The best teams don't argue about TDD vs BDD. They use the right approach at the right level:

- **TDD** for the code
- **BDD** for the features
- **ATDD** for the business

**Tests aren't about proving your code works. They're about proving you built the right thing.**

---

*By [estebanrfp](https://estebanrfp.com) — Full Stack Developer, dWEB R&D*
