TDD V.S BDD
Tags: testing, programming
Published: 2022-12-04 18:39:05

## Introduction

We talked [**Here**](https://dev.to/smuhammed/unit-testing-is-not-just-a-best-practice-46ab) about how important unit testing is in the software development process, and benefits you got from writing unit tests, but here we are comparing between two methodologies which are **TDD** and **BDD**

## Test Driven Development (TDD)

Test driven development is a methodology that aims on writing the test cases of any piece of code before you go ahead and implement it, after that you write the logic and cover the test cases you wrote before (a.k.a red/green methodology), and so on.. By this you are covering all your logic with safety net that will save your back in case any upcoming changes that may break your code, and the quality of your code here is measured by how much percentage you cover from code with unit tests and based on that your code can be pass or fail on automated pipeline.

## The Gap with TDD

We all agree on that the TDD has a lot of advantages that make you follow this methodology while you are developing your product, but on the other hand no matter how much your test cases cover from your logic, there is no guarantee that combining these covered pieces of logic will deliver the desired functionality. Yes each piece of logic does its' job perfectly, but still have no guarantee, and in real software development you don't deliver code, you deliver functionalities/features that what real matters.

## Behavioural Driven Development (BDD)

Behavioural driven development is a new methodology that aims on test too, but not at the the level of methods or piece of code but at the level of functionality/feature, which you are writing what you expect from this feature to deliver to user and cover all scenarios that have this feature to do, then implementing the logic that perform this feature. By this you are protecting the most important part of the software. **Its' features.**

## BDD with Cucumber Framework

Cucumber is a framework that enables the BDD methodology by giving the ability to write a software features in some script files called **feature** file as below and locate these files with source code.

![.feature file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qd02wxvd7x02d4wuiikm.png)

What empowers these files that its' format follow almost the Agile user story format which makes reading and writing them easy even for non-technical persons like scrum masters and product managers and making them more engaged with  development process and code.

Then the developer can take these feature files and start implementing the logic based on them as below example with Java.

![feature file coverage with java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmh3wnp4rpfd9ze3mhp3.png)

By making the predicates more general you can get benefit from reusing them and reduce the amount of code to cover the scenarios.

> There are multiple frameworks beside Cucumber to enable BDD methodology. more details in Resource section  
 

## At the End!

Both two methodologies has its' own benefits, and to get the most safety for our product we should combine both of them, there is no benefit from having 100% unit test coverage, not all pieces of logic have to be covered by unit test, but we can use unit test to cover the most important functions that has a critical job to do and we can cover the rest of code by BDD.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/osnhhkjbg7htl77xeumk.png)


## Resources

- [Cucumber framework](https://cucumber.io/docs/cucumber/) 
- [Cucumber with spring boot example](https://www.youtube.com/watch?v=AHHLDaTVXqg)
- [BDD frameworks](https://www.perfecto.io/blog/bdd-testing-frameworks)

IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.

<a href="https://www.buymeacoffee.com/samehmuh95" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>