Behavior-Driven Development

somphon rueangsri
Software Engineering KMITL
5 min readApr 16, 2020

April 16, 2020.

What is Behavior-Driven Development

Behavior-Driven Development (BDD) is a type of Agile testing Method. It simulates how an application should behave from the end user’s perspective and encourages teams (developers, QA, and non-technical or business participants) to use conversation and concrete examples to formalize a shared understanding of how the application should behave.

BDD begins with natural language to create examples which are called scenarios. These scenarios are an example of how the application should behave from the standpoint of the user. For each feature in the scenarios, it separates into two parts: the narrative part and acceptance criteria part.

Narrative part: A short description of the features with the following structure:

  • As a: Person or role who will benefit from the feature.
  • I want: The features.
  • So that: The benefit or value of the feature.

Feature: withdraws cash

As an: Account Holder

I want: to withdraw the money from the ATM

So that: I can have money without going to the bank

2. Acceptance criteria part: A description of each specific scenario for the narrative part. The scenarios require to be in the Gherkin format, this format consists of 3 component + 1 addition part :

  • Given — Starting State
  • When — What is the user does
  • Then — The expect result
  • And — used to combine two or more same types of activities. Can follow When and Then

Scenario: Account has sufficient funds

Given: the account balance is $100

And: the card is valid

And: the machine contains enough money

When: the Account Holder requests $20

Then: the ATM should dispense $20

And: the account balance should be $80

And: the card should be returned

Behavior-driven development Cycle

It is similar to the Test-driven development (TDD, development technique that focuses more on the implementation of a feature) cycle but it expands on it a bit more. In TDD, the cycle starts with an issue tracker and ends with refactoring. However, BDD expands on the coding phase (see the figure below).

Behavior-driven development tools

A cucumber is a tool based on the BDD framework which is used to write acceptance tests for the web application. It allows automation of functional validation in an easily readable and understandable format (like plain English) to business analysts, developers, testers, etc. Cucumber is available for most mainstream programming languages such as Java, JavaScript, Ruby, Python, etc.

Cucumber consists of 2 parts. The first part is the feature file which is the plain text. It describes the features in Gherkin format so you can create a file similar to example 1 and save with the .feature tag to make it a feature file.

Example 1: feature file

The second part is the step file. This is the file that Cucumber uses for running automated test save with .step

Example 2: step file

Advantages and disadvantages of BDD

Like other productive software development methods, BDD also has its pros and cons. Let’s go over them, starting from the pros:

  • Reduced waste: BDD is focused on discovering and developing features that bring high business value, so any feature that does not bring value will be ignored.
  • Reduced costs: Because BDD reduces waste, the development will only focus on features that matter, which in turn reduces any additional cost.
  • Easier and safer changes: The requirements changes will be easier to implement because of the living documentation. Changes are also safer because of the executable specification that acts as automated acceptance and unit tests.
  • Frequent releases: The releases will become more frequent as the automated tests will reduce some of the manual testings the testers need to conduct. The testers will be able to focus on other more complicated tests and scenarios.
  • Clear vision: As the name suggests, BDD helps you see the behavior you want from the software and aids in brainstorming for identifying future capabilities.

Seeing how BDD is strong at delivering results, some drawbacks are vital for BDD to work:

  • Requiring a lot of communication and collaboration: BDD practices are very focused on discussions and collaboration between all parties involved. If the customer is unable to engage in conversions and give feedback in time, it will be difficult to work past ambiguities and questions generated by the user stories and won’t be taking full advantage of BDD.
  • BDD works best in Agile or iterative-type development methods: One BDD’s principles assumes that it’s very hard to know all the requirements from the beginning and that we don’t have to define them all in the first phase of a project, but instead the knowledge of the stakeholders will evolve during the duration of a project. That’s why it is considered that BDD is only well suited to be used in conjunction with Agile or iterative methodologies.
  • Poorly written tests can lead to higher maintenance costs: For some complex applications, it will require experience to design and write the automated acceptance tests. If poorly written tests were written for such an application, they will become hard to maintain and additional unnecessary costs will be added.

BDD vs Other testing methods/practices

There are also other similar testing methodologies, namely TDD and ATDD. Test-Driven Development (TDD) is a testing methodology or a programming practice implemented from a developer’s perspective where the QA engineer starts designing and writing test cases for every small functionality of an application. The main intention of this technique is to modify or write a fresh code only when the test fails. It aims to answer the question — Is the code valid?

Acceptance Test-Driven Development (ATDD) is a technique where a single acceptance test is written from the user’s perspective. It mainly focuses on satisfying the functional behavior of the system. This technique attempts to answer the question — Is the code working as expected?

So how does BDD stack up against these two? Let’s compare BDD with the other two:

BDD has its purpose, as well as TDD and ATDD. Depending on the kind of project and the results it aims to achieve, the right method (or even a combination of methods) can be implemented to meet specific requirements in most efficient ways.

Summary:-

There are probably many people with the misconception that BDD is naturally a tool framework, but in reality, BDD is a development approach and not a tool framework (but there are also tools for it). The fundamental goal of BDD is to keep the team collaborating and to have a conversation between the teams and the developer for clarification. So the most significant aspect of BDD is not the tools, but the conversations.

--

--