This Tutorial Explains The Differences Between TDD vs BDD With Examples:
TDD or Test Driven Development and BDD or Behavior Driven Development are the two software development techniques.
Before we dive deeper into the difference between these two, let us first understand what do they mean individually and how are they used?
Let’s Start!!
Table of Contents:
What Is TDD?
TDD stands for Test Driven Development. In this software development technique, we create the test cases first and then write the code underlying those test cases. Although TDD is a development technique, it can also be used for automation testing development.
The teams that implement TDD, take more time for development however, they tend to find very few defects. TDD results in improved quality of code and the code that is more reusable and flexible.
TDD also helps in achieving high test coverage of about 90-100%. The most challenging thing for developers following TDD is to write their test cases before writing the code.
Suggested Read => Ultimate Guide for Writing Excellent Test Cases
Process Of TDD
TDD methodology follows a very simple 6 step process:
1) Write a test case: Based on the requirements, write an automated test case.
2) Run all the test cases: Run these automated test cases on the currently developed code.
3) Develop the code for that test cases: If the test case fails, then, write the code to make that test-case work as expected.
4) Run test cases again: Run the test cases again and check if all the test cases developed so far are implemented.
5) Refactor your code: This is an optional step. However, it’s important to refactor your code to make it more readable and reusable.
6) Repeat the steps 1- 5 for new test cases: Repeat the cycle for the other test cases until all the test cases are implemented.
Example Of A Test Case Implementation In TDD
Let’s assume that we have a requirement to develop a login functionality for an application which has username and password fields and a submit button.
Step1: Create a test case.
@Test Public void checkLogin(){ LoginPage.enterUserName("UserName"); LoginPage.enterPassword("Password"); HomePage homePage = LoginPage.submit(); Assert.assertNotNull(homePage); }
Step 2: Run this test case and we’ll get an error that says the Login page is not defined and there are no methods with names enterUserName, enterPassword and submit.
Step3: Develop the code for that test case. Let’s write the underlying code which will enter the username and password and get a home page object when they are correct.
public class LoginPage{ String username; String password; //store username public void enterUserName(String username){ this.username = username; } //store password public void enterPassword(String password){ this.password = password; } //match username and passowrd in db and return home page public HomePage submit(){ if(username.existsInDB()){ String dbPassword = getPasswordFromDB(username); if(dbPassword.equals(password){ Return new HomePage(); } } }
Step4: Run the test case again and we’ll get an instance of the home page.
Step5: Let’s refactor the code to give the correct error messages when the if conditions in the submit method, are not true.
//match username and passowrd in db and return home page public HomePage submit(){ if(username.existsInDB()){ String dbPassword = getPasswordFromDB(username); if(dbPassword.equals(password){ Return new HomePage(); } else{ System.out.println("Please provide correct password"); return; } } else{ System.out.println("Please provide correct username"); }
Step6: Now let’s write a new test case with an empty username and password.
@Test Public void checkLogin(){ LoginPage.enterUserName(""); LoginPage.enterPassword(""); HomePage homePage = LoginPage.submit(); Assert.assertNotNull(homePage); }
Now if you try to run this test case, it will fail. Repeat steps 1 to 5 for this test case and then add the functionality to handle empty username and password strings.
What Is BDD?
BDD stands for Behavior Driven Development. BDD is an extension to TDD where instead of writing the test cases, we start by writing a behavior. Later, we develop the code which is required for our application to perform the behavior.
The scenario defined in the BDD approach makes it easy for the developers, testers and business users to collaborate.
BDD is considered a best practice when it comes to automated testing as it focuses on the behavior of the application and not on thinking about the implementation of the code.
The behavior of the application is the center of focus in BDD and it forces the developers and testers to walk-in the customer’s shoes.
Process Of BDD
The process involved in BDD methodology also consists of 6 steps and is very similar to that of TDD.
1) Write the behavior of the application: The behavior of an application is written in simple English like language by the product owner or the business analysts or QAs.
2) Write the automated scripts: This simple English like language is then converted into programming tests.
3) Implement the functional code: The functional code underlying the behavior is then implemented.
4) Check if the behavior is successful: Run the behavior and see if it is successful. If successful, move to the next behavior otherwise fix the errors in the functional code to achieve the application behavior.
5) Refactor or organize code: Refactor or organize your code to make it more readable and re-usable.
6) Repeat the steps 1-5 for new behavior: Repeat the steps to implement more behaviors in your application.
Also Read => How Testers Are Involved In TDD, BDD & ATDD Techniques
Example Of Behavior Implementation In BDD
Let’s assume that we have a requirement to develop a login functionality for an application which has username and password fields and a submit button.
Step1: Write the behavior of the application for entering the username and password.
Scenario: Login check Given I am on the login page When I enter "username" username And I enter "Password" password And I click on the "Login" button Then I am able to login successfully.
Step2: Write the automated test script for this behavior as shown below.
@RunWith(Cucumber.class) public class MyStepDefinitions { @Steps LoginPage loginPage; @Steps HomePage hp; @Given("^I am on the login page $") public void i_am_on_the_login_page(){ loginPage.gotoLoginPage(); } @When("^I enter \"([^\"]*)\" username$") public void i_enter_something_username(String username) { loginPage.enterUserName(username); } @When("^I enter \"([^\"]*)\" password$") public void i_enter_something_password(String password) { loginPage.enterPassword(password); } @When("^I click on the \"([^\"]*)\" button$") public void i_click_on_the_submit_button(String strArg1) { hp = loginPage.submit(); } @Then("^I am able to login successfully\.$") public void i_am_able_to_login_successfully() { Assert.assertNotNull(hp); } }
Step3: Implement the functional code (This is similar to the functional code in TDD example step 3).
public class LoginPage{ String username = ""; String password = ""; //store username public void enterUserName(String username){ this.username = username; } //store password public void enterPassword(String password){ this.password = password; } //match username and passowrd in db and return home page public HomePage submit(){ if(username.existsInDB()){ String dbPassword = getPasswordFromDB(username); if(dbPassword.equals(password){ Return new HomePage(); } } }
Step4: Run this behavior and see if it is successful. If it is successful, then go to step 5 otherwise debug the functional implementation and then run it again.
Step5: Refactoring the implementation is an optional step and in this case, we can refactor the code in the submit method to print the error messages as shown in step 5 for the TDD example.
//match username and passowrd in db and return home page public HomePage submit(){ if(username.existsInDB()){ String dbPassword = getPasswordFromDB(username); if(dbPassword.equals(password){ Return new HomePage(); } else{ System.out.println("Please provide correct password"); return; } } else{ System.out.println("Please provide correct username"); }
Step6: Write a different behavior and follow steps 1 to 5 for this new behavior.
We can write a new behavior to check if we get an error for not entering the username as shown below:
Scenario: Login check Given I am on the login page And I click on the "Login" button Then I get an error to enter username.
TDD Vs BDD – Key Differences
TDD | BDD |
---|---|
Stands for Test Driven Development. | Stands for Behavior Driven Development. |
The process starts by writing a test case. | The process starts by writing a scenario as per the expected behavior. |
TDD focuses on how the functionality is implemented. | BDD focuses on the behavior of an application for the end user. |
Test cases are written in a programming language. | Scenarios are more readable when compared to TDD as they are written in simple English format. |
Changes in how the application functions impact a lot on the test cases in TDD. | BDD scenarios are not much impacted by the functionality changes. |
Collaboration is required only between the developers. | Collaboration is required between all the stakeholders. |
Might be a better approach for projects which involve API and third-party tools. | Might be a better approach for projects which are driven by user actions. For eg: e-commerce website, application system, etc. |
Some of the tools which support TDD are: JUnit, TestNG, NUnit, etc. | Some of the tools which support BDD are SpecFlow, Cucumber, MSpec, etc. |
Tests in TDD can only be understood by people with programming knowledge, | Tests in BDD can be understood by any person including the ones without any programming knowledge. |
TDD reduces the likelihood of having bugs in your tests. | Bugs in tests are difficult to track when compared to TDD. |
Conclusion
Choosing between TDD Vs BDD can be very tricky. Some might argue that BDD is better for finding bugs whereas the others might just say that TDD gives higher code coverage.
Neither methodology is better than the other. It depends on the person and the project team to decide on which methodology to use.
We hope this article has cleared your doubts about TDD vs BDD!!