DEV Community

Cover image for TDD V.S BDD
Sameh Muhammed
Sameh Muhammed

Posted on

TDD V.S BDD

Introduction

We talked Here 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

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

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

Resources

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

Buy Me A Coffee

Oldest comments (0)