Redsauce's Software QA Blog

BDD: Uniting business and development

Posted by Héctor Sisternas

{1570}

What is BDD?

BDD (Behaviour Driven Development), is a methodology that approaches software development from the end user's perspective. It is about understanding the customer's needs, putting ourselves in their shoes, and designing the software to work properly in various situations, ensuring their satisfaction.


In the world of software development, where innovation and quality are essential to stand out, BDD is a methodology that has been gaining popularity since its introduction in 2006 and is transforming the way we create applications.


If you've ever wondered how to ensure smooth communication between development teams and stakeholders, or how to make sure that every feature delivered brings real value to the end user, then this perspective will surprise you.


This is not just another methodology, it's a philosophy that puts focus on the user, quality and collaboration.

How does BDD work?

BDD puts a layer of natural language on top of the code.

It is a DSL (Domain Specific Language) that simplifies using code (java, ruby, javascript...) by putting Gherkin on top of it.


Some of its main features are:


Cross-team collaboration: BDD promotes collaboration between development, business and quality teams. All team members work together to identify key system behaviors and define them in a common language understandable by all.


Natural language specifications: A key feature of BDD is the expression of requirements and behaviors in a user-friendly way, avoiding technical jargon. The language used is Gherkin, a "Domain Specific Language" (DSL). This makes it easy to understand for both non-technical people and developers, leading to better alignment between teams.


Test automation: Using automation tools, behaviors defined in natural language are translated into executable test scenarios. This ensures that the software meets the stated requirements and behaves as expected, avoiding regressions and bugs.


To effectively implement BDD and take full advantage of its benefits, it is important to have a Gherkin Best Practice Guide (for expressing specifications in natural language) and Cucumber (for automating tests), which are the most popular tools in the BDD methodology.

BDD: Example of use

Let's suppose we are developing an ecommerce and we want to test the process of adding an item to the cart.


First create a file add_to_cart.feature in the root of the project with the following content written in Gherkin (which supports dozens of languages!):

    Feature: Add item to cart
As a customer
I want to be able to add items to the cart
To be able to make an online purchase

Scenario: Successfully add item to cart
Since I am on an item's page
When I add the item to the cart
Then I should see a confirmation message
And the cart should display the added item

Now, we will translate this scenario into executable code using Cucumber in JavaScript language, creating an add_to_cart_steps.js file with the following content:

    const { Given, When, Then } = require('cucumber');
Given("I'm on an article page", function () {
// Code to navigate to the article page
});

When('I add the item to cart', function () {
// Code to add an item to the cart
});

Then("you should see a confirmation message", function () {
// Code to verify that the confirmation message is shown
});

Then("the cart should display the added item", function () {
// Code to verify that the item is displayed in the cart
});

And in this way we can create automated tests based on scenarios written in natural language. This allows effective communication between the non-technical team and developers, improving collaboration and understanding of the tests.

Why use BDD in my project?

Better understanding of requirements: With BDD, all team members have a clear and shared view of what is expected from the software. This minimizes the possibility of misunderstandings (mainly between Product and Programmers), while reducing the risk of developing unnecessary features or not developing the required ones.


Focus on user value: BDD focuses on the value that the software brings to the end user. By defining behaviors from the customer's perspective, it ensures that development is aligned with real needs.


Reduced bugs and costs: Test automation in BDD helps to catch bugs early in the development process, which reduces the cost of fixing problems later in the process.

What is the difference between BDD and TDD (Test Driven Development)?

Both methodologies are popular approaches to improving the quality and efficiency of software development, but have different approaches to achieving their goals.

Similarities between BDD and TDD:

Automated Testing: Both BDD and TDD are methodologies based on automated test execution. In both approaches, tests are written before or simultaneously with code implementation and are automatically executed to verify software behaviour.


Quality orientation: Both BDD and TDD aim to improve software quality by detecting bugs early and ensuring that code conforms to defined requirements.


Incremental approach: Both validate small units of functionality and can be integrated into the project development cycle.

TDD vs BDD:

Testing approach: The main difference between BDD and TDD lies in the testing approach. While TDD focuses on unit tests at the code level to validate the behaviour of functions or methods, BDD focuses on functional tests at the application level, expressed in natural language, from the end user's perspective.


Communication and language: BDD promotes more effective communication between team members and stakeholders by expressing requirements and functionality in natural language, while TDD uses technical language and focuses on more specific and atomic testing of functions at the code level.


Stakeholders: BDD directly involves the project stakeholders, from the technical team to the stakeholders, in the definition of the functionalities.TDD, on the other hand, is a resource used almost exclusively by the development team.


Execution speed: A battery of unit tests programmed using TDD can (and should) take only a few tenths of a second to execute. Tests developed under BDD, on the other hand, are more laborious to program, usually take several minutes even when running in parallel and, if they are not programmed in a resilient way, they are much more fragile. In case you have any doubts, in this post we tell you how to know if your tests are bad.

Can BDD and TDD complement each other?

Of course they can! BDD and TDD can not only complement each other in the software development process, but we strongly recommend it. BDD provides a clear view of the software functionality from the user's point of view, which helps to ensure that development meets real needs. In turn, TDD focuses on validating behaviour at the code level, which ensures the quality and robustness of individual features.


Using both approaches, teams can obtain a broader coverage in software validation, ensuring both the overall functionality and the quality of individual code units.


If you would like to discover what TDD is, with examples so that you can put it into practice today in your project, run to read the post ** TDD: Writing tests first, fearlessly**.

BDD is for you

If you haven't yet tried BDD in your projects, I encourage you to explore it and discover the power it can bring to your team and your applications


Adopting it could be the boost you need to take your development to the next level!Ultimately, we focus development on bringing value to the end user and ensure that every feature, test or line of code contributes positively to their experience.


This and 30 other methodologies and tips are explained in detail in our free ebook.How are BDD and TDD similar?


Start improving your development now:


image

About us

You have reached the blog of Redsauce, a team of experts in QA and software development. Here we will talk about agile testing, automation, cybersecurity… Welcome!