Skip to content

Chapter 2 of 7

TDD and BDD

Test-Driven Development, or TDD, is a development practice in which tests are written before production code. The cycle begins with Red, writing a failing test that describes the desired behavior. The next step, Green, involves writing the minimum code necessary to make the test pass. The cycle concludes with Refactor, where the developer cleans up the code while keeping all tests green. A core rule is never to write production code without a failing test first, and each iteration is meant to be small, on the order of minutes rather than hours.

This Red-Green-Refactor rhythm produces several benefits. Because developers must specify behavior before implementation, TDD forces them to think about the API before diving into the code. Untestable code is often a signal of poor design, such as tight coupling or hidden dependencies, and so writing tests first naturally encourages SOLID principles, dependency injection, and small functions. The result is modular, loosely coupled code that is easier to maintain, extend, and understand. As a side effect, the test suite becomes living documentation, gives confidence when refactoring, and reduces the introduction of bugs.

Behavior-Driven Development, or BDD, extends TDD by expressing tests in natural language that describes behavior from a user's perspective. BDD uses an ubiquitous language shared by developers, QA, and stakeholders, and scenarios are typically structured as Given-When-Then. Tools such as Cucumber, SpecFlow, and Behave execute these human-readable specifications. The scenarios themselves are written in Gherkin syntax, which provides keywords like Feature, Scenario, Given, When, Then, And, and But to describe a workflow. Because BDD focuses on what the system does rather than how it is implemented, it complements TDD by aligning tests with business intent. The Given-When-Then pattern maps directly onto AAA: Given corresponds to Arrange, When to Act, and Then to Assert.

All chapters
  1. 1Test Types and Strategy
  2. 2TDD and BDD
  3. 3Test Doubles
  4. 4Writing and Structuring Tests
  5. 5Test Infrastructure and Frameworks
  6. 6Measuring Test Quality
  7. 7Specialized Testing and CI/CD

Drill it

Reading is not remembering. These come from the Testing Tdd deck:

Q

What is a unit test?

A unit test verifies the behavior of a single, isolated piece of code (e.g., a function or method). Key characteristics:Tests one logical unit in isolationDepen...

Q

What is an integration test?

An integration test verifies that multiple components work correctly together. Unlike unit tests, it exercises real interactions:Tests communication between mod...

Q

What is an end-to-end (E2E) test?

An E2E test simulates real user workflows through the entire application stack. It validates the system from the user's perspective:Uses tools like Playwright,...

Q

What is the Test Pyramid?

The Test Pyramid is a testing strategy that recommends:Base (most): Unit tests — fast, cheap, numerousMiddle: Integration tests — moderate speed and countTop (f...