A beginner's guide to behavior-driven development (BDD)

What is Behavior-Driven Development?

When creating software, particularly new products, a diverse set of individuals is involved, including business and product owners, developers, testers, and QA engineers. Often, these stakeholders perceive the end result differently. Managers and marketers focus on business objectives and user satisfaction, developers work within technical and time constraints to write functional code, and testers and QA engineers concentrate on boundary cases, exceptions, and passing tests. These distinct perspectives can lead to miscommunication and misalignment.

To address these challenges, Behavior-Driven Development (BDD) was introduced. BDD is the next step in the evolution of Test-Driven Development (TDD). It shifts the focus from thinking in tests to thinking in behavior. Imagine transitioning from assembling puzzle pieces to crafting a compelling story. BDD integrates various aspects of software development into a coherent narrative, fostering better communication and collaboration.

BDD serves as a bridge between various stakeholders, fostering collaboration among developers, testers, and customer representatives throughout the project's lifecycle. This approach is particularly effective when combined with an agile methodology, as it facilitates a shared understanding of project goals and requirements.

A key strength of BDD lies in its ability to bridge the gap between technical and non-technical individuals, enabling seamless cooperation among all participants involved in the project. As a result, they share a more unified perception of the system.

The originator of BDD, Dan North, proceeded from the notion that while tests assess the state and operability of a system, they do not provide a comprehensive description of its behavior. This limitation can potentially create a false sense of security regarding the system's functionality.

“I found the shift from thinking in tests to thinking in behavior so profound that I started to refer to TDD as BDD, or Behavior-Driven Development.”

Dan North, Introducing BDD.

Rather than writing code solely to pass tests, BDD involves writing code that embodies specific behaviors. These behaviors are articulated using natural language constructs that are easily comprehensible, even to those without a technical background.

One can view BDD as an extension of TDD, catering to a broader audience, including testers, analysts, and project managers. BDD stands out for its unique ability to harmonize technical intricacies with business objectives, while also considering the system's behavior from the user's perspective. This distinct approach assists developers and business stakeholders in achieving a shared comprehension of product requirements.

How Does Behavior-Driven Development Work?


Behavior-Driven Development operates through a structured process that enhances collaboration and ensures alignment among project participants. Here's how BDD works:

Establishing common understanding: At the inception of the project, the involved participants collaboratively define and adopt a shared vocabulary and fundamental concepts. This foundation later aids developers in naming classes and methods cohesively.

Defining desired behavior: Subsequently, the team proceeds to articulate the desired behavior of the system. This behavior holds intrinsic business value and is usually aligned with targeted business outcomes. These outcomes establish benchmarks for the team's objectives, facilitating a focused and purposeful direction.

Ranking business outcomes: In-depth deliberations take place among the participants to analyze and rank these business outcomes by their relative significance. This prioritization empowers developers with a clear sense of which aspects require initial attention and implementation.

Translating to user stories: The team then transforms prioritized features into user stories. These user stories encapsulate the dynamic interactions between users and the system, capturing specific scenarios. Importantly, these user stories may manifest different variations contingent on contextual factors.

Envisioning scenarios: Within the BDD framework, these contextual variations are termed scenarios. Scenarios serve as acceptance criteria, guiding the development process by providing specific instances that the software must address.

Feature files and domain-specific language

BDD places a strong emphasis on effective communication within a team, ensuring a clear and shared understanding of software functionality. This is achieved through the utilization of consistent business terminology and a unified language. This common language not only streamlines communication within the team but also reduces the learning curve for new team members, ultimately benefiting the business.

Central to BDD is the concept of “ubiquitous language.”

“Ubiquitous language is a language whose structure comes from a domain model. It contains the terms which will be used to define the behavior of a system.”

— Solis Pineda, Carlos & Wang, Xiaofeng. (2011). A Study of the Characteristics of Behaviour Driven Development. Proceedings - 37th EUROMICRO Conference on Software Engineering and Advanced Applications, SEAA 2011. 383 - 387. 10.1109/SEAA.2011.76.

In BDD teams, participants convene to collaboratively discuss and document software specifications that garner unanimous agreement. These documents, known as feature files, describe the various functionalities of the software. These feature files are written in the Gherkin language (further details below) and serve as repositories for system requirements and scripts. Feature files are typically saved with the “.feature” extension.

Feature files, due to their structured format, also double as test definitions, which can be automated using tools like Cucumber — a widely adopted program in behavioral-oriented development.

A standard feature file encompasses:

1. Feature name: Clearly indicating the feature being described (e.g., Payments).

2. User story for the feature: Describing a specific user interaction with the feature.

3. Acceptance criteria: Outlining conditions that must be met for the feature to be considered successful.

4. Relevant links: Including any useful references for developers.

5. Gherkin scripts: Detailed scenarios written in Gherkin language, which describe user interactions and expected system behavior.

Feature files create organized test suites for features, making regression testing easier. Each feature iteration involves a new story and test, maintaining up-to-date system expectations. Automated tests compare code to feature files, flagging mismatches for quick issue detection. Analysts use feature files to introduce new requirements, reducing the effort of drafting tests. This aligns developers with acceptance criteria and streamlines requirement additions.

For various technology stacks, an array of tools is available to parse and execute feature files written in the Gherkin language. In Ruby, you encounter tools like Turnip, Spinach, and Cucumber; in .NET, there's SpecFlow; for Java, JBehave; and Behat for PHP, among many others.

Writing test scenarios in Gherkin syntax

Behavior-Driven Development doesn't enforce rigid rules, but it emphasizes a standardized set of phrases that encompass behavioral specifications. In 2007, Dan North introduced the Gherkin language, a widely accepted specification template within BDD.

Gherkin serves as the foremost Domain-Specific Language (DSL) for articulating behavior in BDD. It's employed across over 70 languages to detail system behavior in specific scenarios, often using the term “should” to highlight the behavior's essence.

“A more subtle aspect of the word ‘should’ becomes apparent when compared with the more formal alternatives of ‘will’ or ‘shall’. ‘Should’ implicitly allows you to challenge the premise of the test: “Should it? Really?” This makes it easier to decide whether a test is failing due to a bug you have introduced or simply because your previous assumptions about the system's behavior are now incorrect.”

Dan North, Introducing BDD.

In BDD, each behavior is captured as a concise, one-line “Scenario.” It's a foundation for a behavioral specification, expressed through a sequence of steps similar to a test case.

Gherkin employs essential keywords to structure behavior descriptions: Given, When, Then, And, But, Scenario, Feature, Background, Scenario Outline, Examples. These keywords facilitate communication and documentation of software behavior in a cohesive manner.

Gherkin offers two primary advantages:

1. Enhanced communication: Gherkin facilitates acceptance test description using language resonating with developers, QA professionals, and business stakeholders, promoting a deeper understanding of software expectations.

2. Tight alignment: Gherkin establishes a direct link between acceptance tests (GIVEN/WHEN/THEN statements) and automated tests. Modifications to acceptance tests are flagged by corresponding Cucumber tests, ensuring software alignment with business specifications, fostering a cohesive development process.

The “Given When Then” approach

The “Given When Then” approach in software development provides a clear and understandable technique, even for non-programmers. It's like crafting a recipe for cooking, ensuring the software behaves as expected.

  • Given the ingredients and tools you have in your kitchen,
  • When you follow a series of steps, combining and cooking the ingredients,
  • Then you end up with a delicious and satisfying meal.

In software development, it's similar.

  • Given a certain initial state of the system,
  • When specific actions or events occur,
  • Then the software behaves in an expected manner, producing the desired outcomes.

This approach describes software aspects like a story, created through team discussions. Business Analysts transform concepts into a cohesive story, testers assess component significance, and developers evaluate implementation strategies.

Creating these stories is incremental. Initially, software requirements might be unclear, but developers and testers clarify costs and benefits, deciding which aspects are necessary.

The “Given When Then” approach fosters universal understanding, ensuring software aligns with everyone's vision.

Using predefined templates

BDD employs predefined templates for features, user stories, and scenarios, ensuring consistent, comprehensible descriptions. For example:

Scenario 1: User makes a purchase

  • Given the user is logged in
  • And the user has sufficient funds in their account
  • When the user initiates a purchase
  • Then the user's account balance should decrease
  • And an invoice should be generated

The breakdown:

  • Given: Sets the system state (e.g., user logged in, available funds).
  • When: Represents the user action leading to a result.
  • Then: Describes the outcome, revealing system behavior.

This structured description enhances clarity, aligns behavior specs with implementation, and quickens understanding of tested behaviors. By condensing and reorganizing, the text maintains its key points while becoming more concise and readable.

Learn more about Gherkin.

BDD test scenario examples

Here are a few examples following the Feature-Scenario structure of BDD, along with the Given-When-Then approach.


Online shopping cart

Feature: Manage shopping cart

As an online shopper,
I want to add and remove items from my shopping cart,
So that I can review and purchase the items I want.

Scenario: Adding items to the cart

Given the user has selected a product
When the user adds the product to their cart
Then the cart should display the added product

Scenario: Removing items from the cart

Given the user has items in their cart
When the user removes a product from the cart
Then the cart should not display the removed product

User authentication

Feature: User authentication

As a website user,
I want to be able to create an account and log in,
So that I can access personalized content.

Scenario: User registration

Given the user is on the registration page
When the user submits valid registration details
Then the user should receive a confirmation email

Scenario: User login

Given the user has a registered account
When the user enters valid login credentials
Then the user should be redirected to their dashboard

Advantages of behavior-driven development

BDD offers compelling advantages for software development, ensuring high-quality results.

  • Enhanced collaboration: BDD fosters improved interaction, aligning teams and reducing uncertainties.
  • Flexible integration: Unlike TDD, BDD seamlessly integrates at any stage, enabling easy script modifications.
  • Inclusive participation: Comprehensible tests allow diverse members to join, promoting inclusivity.
  • Efficient specification: BDD minimizes documentation, employing concise specifications.
  • Business engagement: BDD's accessible language enhances transparency, engaging non-technical stakeholders.
  • Cross-language compatibility: BDD's language-agnostic nature streamlines transitions.

For business users:

  • User-friendly requirements: Non-technical users articulate needs using Gherkin.
  • Reduced misinterpretation: Intuitive language minimizes requirement ambiguity.
  • Simplified reporting: Clear feature files aid management reporting.

BDD strengthens collaboration, empowers stakeholders, and leads to successful outcomes.

Disadvantages of behavior-driven development

  • Shift in tester involvement: BDD requires early tester engagement, altering traditional practices and demanding additional coordination.
  • Extended deployment time: Emphasis on collaboration and scenario creation may extend deployment timelines, impacting schedules.
  • Increased project complexity: BDD can introduce project complexity, especially in subsequent code refactoring.
  • Test development and maintenance complexity: BDD adds new dependencies, making test development and maintenance intricate.
  • Syntax and keyword familiarity: Adherence to specific syntax rules and familiarity with keywords is crucial, posing challenges for some team members.
  • Communication and collaboration dependency: Successful BDD relies heavily on effective communication and collaboration, inadequate cooperation may lead to misunderstandings.
  • Experienced leadership requirement: BDD implementation benefits from experienced leadership to navigate nuances.

Consider these disadvantages alongside benefits and context. BDD might not fit well for:

  • Simple and small projects: BDD overhead might outweigh benefits, favoring lightweight approaches like TDD.
  • Highly technical projects: BDD might not significantly benefit projects focused on technical complexity.
  • Legacy codebases: BDD could be challenging to implement in legacy codebases with undefined requirements and limited documentation.

Behavior-driven development requires a strategic approach

Behavior-Driven Development introduces a profound paradigm shift in software development, fostering enhanced collaboration and a shared understanding among stakeholders. Its "Given When Then" approach, analogous to a structured recipe, delineates the software's behavior in a comprehensible manner.

BDD's cornerstone lies in feature files and the Gherkin language, enabling a cohesive representation of desired software behavior. This structured framework facilitates effective communication, reduces learning curves, and promotes better alignment between technical and non-technical team members.

Despite its merits, BDD brings challenges. It demands early tester involvement, potentially altering established workflows. Moreover, the comprehensive scenario-based approach could extend deployment timelines and introduce complexities, necessitating experienced leadership for successful implementation.

Incorporating BDD offers tangible benefits and considerations. It empowers teams to transcend silos, creating a shared language that bridges gaps and unifies visions. However, it requires a balanced assessment of its advantages against potential disruptions and demands a strategic approach for seamless integration.

You've successfully subscribed to Qase Blog
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.