Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 60 ยท Automation Concepts and Process

TDD vs BDD and Shift-Left

Learn test-driven development (red, green, refactor), behavior-driven development (Given, When, Then), how they differ, and how shift-left testing ties them together.

Traditionally, tests come after the code: build the feature, then check it. Test-driven development (TDD) and behavior-driven development (BDD) flip the order. The test (or the behavior description) comes first and steers the design of the code. Both are part of a bigger idea, shift-left: find problems as early as possible. This lesson explains each practice, shows them in runnable code, and clears up when to use which.

You will meet the vocabulary in Agile Testing and CI, and the original idea of shifting left in Testing in the SDLC.

Test-driven development

TDD is a developer practice with a tiny, repeated loop called red, green, refactor:

  1. Red. Write a small test for behavior that does not exist yet. Run it and watch it fail. (A test that cannot fail proves nothing.)
  2. Green. Write the simplest code that makes the test pass. Do not add anything extra.
  3. Refactor. Clean up the code (and the test) while keeping every test green.

Then repeat with the next small behavior. Each loop takes minutes.

flowchart LR
  R["RED<br/>write a failing test"] --> G["GREEN<br/>make it pass, simply"] --> F["REFACTOR<br/>clean up, stay green"] --> R

The loop is short on purpose: you are never more than a few minutes from a working state.

Why people use TDD

  • The code becomes testable by design, because it had to be tested before it existed.
  • Tests double as executable examples of the intended behavior.
  • A growing safety net makes refactoring less scary and catches regressions.
  • You write only code that a test demands, so there is less unused code.
  • Small steps give constant feedback and make debugging easier.

Its limits

TDD does not guarantee good design, and it does not replace other testing: unit tests do not show that components work together or that the product fits user needs. Teams also vary: some practice strict TDD, others write tests right after the code ("test-after") and still keep the benefits of a good suite.

TDD by example: a tiny kata

The classic exercise "FizzBuzz" is a good short demonstration. The stand-in below shows three loop iterations in one file: tests as plain functions, the failing test first (simulated), then the code growing.

JS Red, green, refactor on a small function

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first run fails because fizzbuzz is not defined at that point of the script, so the "red" step is a real failure. Notice that the earlier tests stay in the file and keep protecting behavior as the code is refactored.

Behavior-driven development

BDD grows out of TDD but changes the focus from "tests" to "behavior" and from "developer only" to "the whole team". Product owners, testers and developers hold a short conversation (often called the three amigos) and agree on concrete examples of how a feature should behave. Those examples are written in plain language and become both the specification and the automated tests, hence the phrase living documentation.

Given, When, Then

BDD scenarios use a fixed structure, often in a language called Gherkin:

TEXT
Feature: Free shipping

  Scenario: Order over the threshold ships free
    Given a cart containing items worth 120 dollars
    When the customer goes to checkout
    Then shipping costs 0 dollars

  Scenario: Order under the threshold pays shipping
    Given a cart containing items worth 30 dollars
    When the customer goes to checkout
    Then shipping costs 5 dollars
  • Given sets up the context (the state before).
  • When is the action or event.
  • Then is the observable outcome.
  • And / But continue a step.

Tools such as Cucumber, SpecFlow/Reqnroll, Behave and similar frameworks match each plain-language step to code (step definitions) and run the scenarios. The value is in the conversation and shared language; the tool is secondary. A team that writes Gherkin alone, without business input, gets the extra layer without the benefit.

Good scenarios describe behavior at the business level, not UI clicks: "When the customer checks out" rather than "When I click the blue button at x=300".

A runnable Given/When/Then

The stand-in below implements a tiny BDD runner: scenarios as text, step definitions as regular expressions mapped to functions. It is a teaching model of how Cucumber-like tools work.

JS A tiny Given/When/Then runner

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Edit value >= 100 to value > 100 and run it: the scenario still passes, because the examples do not test the boundary at exactly 100. That is a reminder that BDD examples are examples; add boundary cases using boundary value analysis.

TDD vs BDD

TDD BDD
Focus Correct, well-designed code units Shared understanding of behavior
Who writes Developers Whole team (business, test, dev)
Language Programming language, unit test framework Plain language (Given/When/Then) plus step code
Level Mostly unit and component Feature or acceptance level
Tools JUnit, pytest, Jest, NUnit Cucumber, Reqnroll, Behave
Output Tests and a clean design Executable specification and living documentation
Main risk Over-testing implementation details Verbose, brittle scenarios nobody reads

They are complements: BDD scenarios define what the feature should do at the edge of the system, and TDD drives the inner code that makes them pass. A related term, ATDD (acceptance test-driven development), writes the acceptance tests first, often as the same kind of examples.

Shift-left testing

Shift-left means moving quality activities earlier in the timeline. TDD and BDD are shift-left practices, along with:

  • reviewing requirements for testability (Requirements Testability);
  • static analysis and linters on every commit;
  • unit tests and fast checks in the CI pipeline;
  • testers joining planning and refinement meetings;
  • early security and performance checks.

The reasoning is the cost curve from What Is Software Testing?: a defect in an idea costs minutes, in production days. Shift-left does not remove later testing; it adds earlier feedback so later testing finds less. Its counterpart, shift-right, tests in production by monitoring and controlled rollouts.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the correct order of the TDD cycle?

    Write a failing test (red), make it pass simply (green), then clean up while staying green (refactor).
  2. 2. Why should you watch a new TDD test fail first?

    A test that has never failed might be checking nothing.
  3. 3. In 'Given a cart worth 120, When the customer checks out, Then shipping is 0', which part is the action?

    Given is context, When is the action or event, Then is the expected outcome.
  4. 4. What is the main added benefit of BDD over plain TDD?

    BDD's value is the conversation and shared language; the automated scenarios are living documentation.

Summary

  • TDD loops through red, green, refactor with tiny steps and yields testable design and a safety net.
  • BDD describes behavior as Given/When/Then examples agreed by the whole team and can run them as automated tests.
  • TDD works at code level, BDD at feature level; they complement each other, and ATDD writes acceptance tests first.
  • Shift-left means finding problems earlier with reviews, static checks, early tests and tester involvement.

Next up: Unit Testing Frameworks and Stubs/Drivers.