TDD and BDD - Know The Difference!
When we develop software we want our software to be reliable and solve user problem, to achieve that we as developer often use difference techniques and approach. TDD (Test Driven Development) and BDD (Behavior Driven Development) is common approach used when developing software. These approaches play a significant role in ensuring the reliability and user-centricity of the software being developed.
Test Driven Development
TDD or Test Driven Development is a process to writing a test before writing the functional code, the result of each test will help the developer to improve the quality and the correctness of code functionality. Test cases in TDD is against specific portion of code, so its often to use unit test to run the tests and make the code dependable.
The brief example of TDD is when we want to develop application to measure length of the line with two coordinate points, the first step we do is creating unit test that assert the given result by the calculate line length function.
@Test
public void givenTwoPoints_whenCalculateLength_thenReturnLength() {
double actual = CalculateLineLengthUseCase.execute(1, 1, 4, 5);
double expected = 5;
assertEquals(expected, actual);
}
Of course the first iteration will be fail, because we don't have the actual function yet, so next step is to adjusting the code to remedy the failure. After we passed the test we need to refactor our code to make it clean, concise and optimized.
Benefits
Test driven development makes sure that when we write new code, we first check that the existing code works well. If there are problems, we stop writing new code until we fix them. This helps us spend less time fixing mistakes later on.
When tests look closely at small parts of the code, developers quickly know what needs fixing, so they can make changes faster.
The code becomes more flexible and easier to maintain because each part is tested before moving to the next step in development. This means the code keeps working well and can be changed easily later on.
Behavior Driven Development
The Test Driven Development approach start the development from testing the specific portion of functional code, on other hand Behavior Driven Development is more user-centric. The first BDD step is to determine user behavior that developer need to create using Gherkin Syntax.
Gherkin uses a set of special keywords to give structure and meaning to executable specifications.
Gherkin Syntax consist of Given - When - Then statement, each of this statement give guide to developer on how they should develop the feature that fulfills behavior that described by point of view of user.
Let's use the example before, when we want to develop application to measure length of the line with two coordinate points. The first step we do is create the scenario using Gherkin.
Feature: Measure length of line
Scenario: User calculate the length of line
Given the User input two points
When the User finish inputing and press "Enter" or "Return" key
Then the length of the line showed on User screen
Scenario: User not input the right format
Given the User input text instead points
When the User press "Enter" or "Return"
Then error message should appear
After the scenario completed, the developer can start writing code based on this. This scenario help the developer to expect what the user input, when we need to start processing the input and what should user get after the calculating process.
Benefits
BDD pays attention to how users feel about the product. This helps the team see things from a wider view and notice if they're missing anything.
Since BDD prioritizes what's important for users, developers, and investors, it helps use resources wisely when making programs.
Popular Behavior Driven Development Framework
-
Cucumber is a tool that supports Behavior-Driven Development (BDD). Cucumber reads executable specifications written in plain text and validates that the software does what those specifications say. The specifications consists of multiple examples, or scenarios.
The Difference
The primary difference between TDD and BDD is what we test, where we start to test, and how we testing that. While TDD testing the specific portion of functional code using unit test through test-first approach, on other hand BDD testing the application behavior using end-user standpoint. This different approach of TDD and BDD make each of techniques had different level of abstraction, TDD focus on low-level unit test of small part of the app verify correctness of individual units, while BDD focus on high-level test that simulate user interactions.
Other difference we notice here is the development workflow, when we using TDD the test must be written before implementing actual code, while BDD we write the scenario collaboratively before implementing actual code.
Let's Wrap Up!
There is no silver bullet when selecting development techniques, every approach had pro and cons it is all depends on what project we working on. Our job as developer is to determine which approach will gave us more benefit. TDD will avoid us from rework cycle because before we move to work on other function, we must make all the test cases passed first. BDD determine higher level requirement with end user standpoint, this approach will gave the developer more flexibility and creativity on how they develop the low level unit.
That's all, Thanks for reading this far ✌️. I'm eager to hear your thoughts about Test Driven Development and Behavior Driven Development.