Difference between TDD and BDD


Test-driven development is a software development approach where the development is focused on the development of unit components and passing of its corresponding unit test. The developer writes the unit test and then writes the code to pass the unit test and refactor the code. The cycle goes on until all the unit components are passed. Whereas behavior-driven development is focused on the development of the behavioral aspects of the system. The entire system has been factored in terms of different behaviors and features, and development is progressed towards the implementation of behaviors.



The below image depicts a typical TDD approach test driven development




The unit test is written by the developer whereas behavior or feature is written usually by the business owner, product owner, or business analyst or usually a non-technical person in a pure English form in the format of- Given, When, Then. The focus is on implementing and validating behavior in BDD, whereas in TDD, the focus is on implementing and validating individual unit components.



BDD


The tools or frameworks used in the TDD approach are TestNG, Junit, Nunit whereas frameworks that supports the BDD approach are Cucumber, Jasmine, etc.


The feature here is nothing but a combination of Context, Event/action, and the Expected outcome. This combination collectively defines the behavior of a particular feature of the entire system.


The Given statement provides a context of what has been given as a precondition,
When is an action that has been performed in the given context
and Then statement is the expected outcome of the performed action in the given context.
The entire system is composed of numerous such features.


These features are written by a business stakeholder and understood by non-technical stakeholders as well. The development team's responsibility is to implement the step definition of these features. The step definition is the technical structure of the given feature.




TDD BDD
Focus is towards implementation of an individual unit component Focus is towards implementation of a behavior of an individual feature
The functionality of the unit component is validated Scenarios are validated
The developer writes the unit test Business analyst or Product owner write the features and scenarios
Each passing unit test ensures all the unit component are working fine Every passing feature ensures all the expected behaviors of all the features are working fine
JUnit, TestNG, NUnit are some of the frameworks used in TDD Cucumber, Jasmine are some of the frameworks used in BDD
White-box testing is involved Black-box testing is involved
It is about 'Doing the thing right' It is about 'Doing the right thing'
Test readability is limited to the developer and technical person Test readability is extended to non-technical persons as well
A unit test cannot be written after the development The behavioral test can be written any time i.e. before, during, and after development