The Differences Between TDD Vs BDD

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?

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.

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.

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.

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.

Step6: Now let’s write a new test case with an empty username and password.

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.

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.

Step2: Write the automated test script for this behavior as shown below.

Step3: Implement the functional code (This is similar to the functional code in TDD example step 3).

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.

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:

TDD Vs BDD – Key Differences


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.


Add a Comment

Your email address will not be published. Required fields are marked *