Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 37 ยท BDD Concepts and Gherkin

BDD Basics: Behavior Before Code

Learn what Behavior-Driven Development is, how it differs from TDD and ATDD, the discover-formulate-automate cycle, and why examples beat requirements documents.

Behavior-Driven Development is a way of building software around conversations about examples. Cucumber is just one tool that supports it. If you only learn the tool you will write beautiful test files that nobody reads; if you learn the practice first, the tool becomes obvious. This lesson explains the practice.

Where BDD came from

BDD grew out of Test-Driven Development (TDD). Practitioners noticed that the word "test" misled people: it sounded like something done after the code, by testers. They shifted the vocabulary to behavior: what should the system do, for whom, in which situation? A specification of behavior can be written before the code, discussed by non-programmers, and later executed.

Three practices are easy to mix up:

Practice Main question Written by Typical size
TDD Is this small piece of code correct? Developers One function or class
ATDD (acceptance TDD) Does the feature meet the agreed acceptance criteria? Team, with a customer representative One user story
BDD Do we all share the same understanding of the behavior? The whole team, in a shared language One business rule or scenario

BDD includes ATDD's acceptance tests but insists on the shared language and the conversation. TDD still happens underneath it: while implementing a scenario, developers write ordinary unit tests too.

The three phases

BDD is usually described as a cycle with three phases.

flowchart LR
    D["1. Discover<br/>talk through examples"] --> F["2. Formulate<br/>write them in Gherkin"]
    F --> A["3. Automate<br/>connect them to the system"]
    A --> D

The cycle repeats for every story; most of the value comes from phase 1.

  1. Discovery. Before any code, people with different perspectives (business, development, testing) explore a story through concrete examples. This is where misunderstandings are found while they are cheap.
  2. Formulation. The agreed examples are written down in a structured, readable form. That form is Gherkin.
  3. Automation. Developers wire each sentence to code so the examples run as tests and keep running forever.

If you skip discovery and only formulate and automate, you have not done BDD; you have written UI tests with extra syntax. The usual cause of disappointing adoptions is exactly that: no shared conversation.

Why examples beat requirements

Consider the requirement "Premium customers get a discount on large orders." It reads fine and is hopelessly vague. Ask three questions: what is a large order, how much is the discount, and does "premium" include a customer whose membership expired yesterday?

Concrete examples force answers. Try it in code. The stand-in below runs the same examples against two plausible readings of a rule. The disagreement appears at one boundary value, which is exactly the sort of case a conversation should settle.

Java Stand-in: examples expose two readings of one rule

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The third example is the one that matters. Before BDD people noticed such boundary questions late (in testing, or from a customer complaint). With discovery they surface in a meeting, and the answer becomes a scenario: "an order of exactly 100 dollars gets 10 percent".

A shared, ubiquitous language

BDD borrows an idea from domain-driven design: use the same words in conversations, in the feature files and in the code. If the business says "policy holder" and the code says Customer, translation errors creep in. Good Gherkin uses the business vocabulary, and good step definitions use it for method and class names too.

Outside-in development

BDD usually works outside-in. You start with a scenario that describes the behavior as a user sees it. It fails because nothing exists. You then work inward, writing the code and the unit tests needed to make it pass, and when the scenario is green the story is done.

flowchart TB
    S["Scenario fails<br/>(red)"] --> U["Write a failing unit test"]
    U --> C["Write code to pass it"]
    C --> R["Refactor"]
    R --> Q{"Scenario passes?"}
    Q -- "no" --> U
    Q -- "yes" --> G["Scenario green: story done"]

Two nested loops: a slow outer loop (the scenario) and a fast inner loop (unit tests).

This is why BDD does not replace unit testing. Scenarios cover business behavior at a coarse grain; unit tests cover the details of each class at a fine grain. A healthy project has many unit tests, fewer service-level tests and only a thin layer of scenarios that run through the UI.

Living documentation

A well-kept suite of scenarios is documentation that cannot lie: if the behavior changes and nobody updates the scenario, the build goes red. Teams often publish the passing scenarios as a readable website. The stand-in below generates a tiny version: it runs each scenario's check and prints a documentation page with a status next to each.

Java Stand-in: living documentation from executable examples

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The third rule is documented but not true of the system. A normal wiki would hide that; executable documentation shows it immediately.

Common misconceptions

  • "BDD is testing with Cucumber." No. BDD is the collaborative practice; Cucumber is one automation tool. You can do BDD without Cucumber and use Cucumber without doing BDD.
  • "Testers write all the scenarios." The point is that the whole team writes and reviews them; the tester contributes a questioning mind, not sole ownership.
  • "Every test should be a scenario." Scenarios illustrate business rules. Edge cases of a parsing function belong in unit tests.
  • "More scenarios mean more quality." A few dozen well-chosen examples per feature beat hundreds of near-duplicates that slow the build and confuse readers.

The next lesson looks at the meeting where good examples come from: the three amigos.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three phases of the BDD cycle?

    Teams discover behavior through examples, formulate them in a shared structured language, then automate them.
  2. 2. Which statement about BDD and Cucumber is correct?

    BDD is a collaborative practice; Cucumber automates the examples but is not the practice.
  3. 3. Why are concrete examples better than a vague requirement?

    An example fixes a specific input and output, so disagreements become visible.
  4. 4. What does outside-in development mean?

    The scenario drives the work; unit tests and code fill in the details until it passes.

Summary

  • BDD is a collaborative practice built on discovering, formulating and automating examples.
  • It extends TDD and ATDD with a shared language that business and technical people both use.
  • Examples expose ambiguous rules early; most of the value is in the conversation.
  • Scenarios stay coarse-grained; unit tests cover the details inside the outer loop.
  • Passing scenarios form living documentation that cannot silently go stale.

Next: The Three Amigos and Example Mapping