Home / BDD

TDD VS BDD

Rating: 5.0Blog-star
Views: 506
by Raunaak
Last modified: October 13th 2021

Driven development, simply put, is the software development process that differentiates software development to work in parallel, more minor, or also in specific sequential steps. TDD and BDD are different methods or practices to test the development and behavior of applications curated by developers for users and stakeholders.

In this article, we have enlightened the essential aspects of TDD vs BDD which will help you understand both Test-Driven Development (TDD) and Behavioral Driven Development (BDD).

TDD vs BDD - What’s the Difference Between TDD and BDD?

The following topics will be covered in this TDD vs BDD:
  1. What is TDD?
  2. TDD Process
  3. Use Cases of TDD
  4. What is BDD?
  5. BDD Process
  6. Use Cases of BDD
  7. TDD vs BDD: Difference Between OLTP and OLAP

First, let us begin exploring the very essence of Test-driven development (TDD).

What is Test-Driven Development(TDD)?

Test-driven development is a revolutionary approach that dates back to 2003. The TDD amalgamates the development of the first-ever written test to result in a production code alongside refactoring. The primary goal of TDD is validation and specification about the requirement and designing aspect before a fully functional code is written. TDD is quite an essential aspect of bringing the best in agile requirements and agile design techniques.

As notable programming technical, the goal of TDD is to curate clean code which works in real-time.

The Entire Process of TDD can be Further Classified into Numerous Steps:

  1. A developer writes a wholly automated test case by considering requirement documents.
  2. A team of developers often runs the test scripts right against which is being put into development. The idea is to see if these tests fail anyhow. These tests are always meant to fail as they do not go through implementation at the beginning.
  3. The development team comes up with a functional code that further ensures the automated test scripts are greenlit.
  4. Lastly, it is up to the development team, who has the right to organize and refactor the entire code. Further, the team can produce an entirely tested deliverable at the sprint’s end.
  5. Test-driven development is usually written in Ruby, Java, among others. Developers can also write TDD by using automation tools like Windmill, Watir, Selenium, among others. As test scripts come from programming languages, it is tough for test owners and business analysts for verifying the core of the test scripts.
If you want to enrich your career and become a professional in Behavior Driven Development (BDD), then visit Mindmajix - a global online training platform: "Behavior Driven Development Online Training" This course will help you to achieve excellence in this domain.

TDD Process

The entire methodology of TDD focuses on a straightforward 6-step process:

  1. Writing Test Case: As per requirements, a test case with complete automation is put forward.
  2. Running Test Cases: These test cases run on the already developed code.
  3. Developing Codes for Test Cases: If the test fails, the code is further written to ensure test cases work as per the requirement.
  4. Running Test Cases upon Completion: Running the test cases one more time to check if the implementation of test cases works correctly.
  5. Refactoring Codes: This step is entirely optional. But, it does add value to testing as refactoring the code makes it both reusable and readable.
  6. Repeating steps 1-5 for brand new test cases: Repeat the entire cycle for other test cases till all the test cases witness an implementation.

Use Cases of TDD

Let’s pretend that there is a requirement for curating a decent login functionality for an application with a username and password. Take a look at the below to figure how the process works before digging about it in detail:

use-cases-of-tdd

Example of Test Case Implementation In TDD

First Step: Creating test case

@Test
Public void checkLogin(){
   LoginPage.enterUserName("UserName");
   LoginPage.enterPassword("Password");
   HomePage homePage = LoginPage.submit();
   Assert.assertNotNull(homePage);
}

Second Step: The developer has to run the test case and ultimately get an error, suggesting that the login page isn’t explicitly defined. Furthermore, there aren’t any methods with contrast to names, enterUserName, enterPassword, and submit.

Third Step: Developing a code for the said test case. The code should be written to enter both username and password. Moreover, writing the code should also aim at getting a homepage object after it is deemed 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 password in db and return home page
public HomePage submit(){
if(username.existsInDB()){
String dbPassword = getPasswordFromDB(username);
if(dbPassword.equals(password){
Return new HomePage();
}
}
}

Fourth Step: The test will have to run again. Upon rerunning the test, users will get a proper example of the said home page.

Fifth Step: Refactoring the code to provide ideal error messages when they submit method conditions aren’t actual.

//match username and password 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");
}

Sixth Step: In the final step, the developer has to write a completely new test case with an empty password and username

@Test
Public void checkLogin(){
LoginPage.enterUserName("");
LoginPage.enterPassword("");
HomePage homePage = LoginPage.submit();
Assert.assertNotNull(homePage);
}

Remember, running this test case would ultimately showcase that it's a failure. It is essential to repeat the steps from 1 to 5 and then add functionality for handling empty password and username strings.

What is Behavioral Driven Development(BDD)?

BDD or Behaviour Driven Development is a practice of refinement and synthesis. BDD has its origin from both Test-Driven Development or TDD and Acceptance Test-Driven Development (ATDD). BDD works in a very different way from TDD. Instead of writing cases, BDD focuses on writing behaviours. Later the code is developed that is required for the application to perform a specific behaviour.

BDD comprises the following tactics making it an effective practice:

  • BDD often follows the essential "Five Whys'' principle to reach the user story. Further, this suggests that BDD's purpose is to oversee business outcomes very clearly.
  • With BDD, it is easier to think from the "outside to the inside." In simple words, BDD doesn't only implement the behaviours that contribute to the business outcomes while eliminating waste.
  • BDD tends to describe behaviours in a single notation that provides direct access to testers, domain experts, and, lastly, developers for an enhanced rate of communication.
  • These techniques could be applied to the lowest abstractions levels of software. It pays a designated amount of attention to behavioural distribution by ensuring evolution remains downright cheap.

BDD Process 

BDD methodologies also comprise six different steps, which share a peculiar similarity with TDD. Let's dig in!

  • Writing an application behaviour: The behaviour of any application is often written in simple English, like a language used by a product owner, business analysts, etc.
  • Writing automated Scripts: Then, the simple English is further converted into a set of programming tests.
  • Implementing functional code: With this step, the operational principle of any underlying behaviour is implemented.
  • Evaluate if the behaviour is thriving: The behaviour is run to see whether it is successful or not. If the behaviour is successful, you could move to the following behaviour to fix errors in functional code to achieve the desired behaviour.
  • Refactor and organize code: Organize or refactoring the code by making it insanely readable and reusable.
  • Repeat steps 1-5 for a new behaviour: Make sure to repeat steps for implementing more behaviours right amid application.

BDD Use Cases

Here, you can find out the requirement for developing a robust login functionality by using BDD.

First Step: The first step requires a behaviour for the application to enter both usernames and passwords.

Second Step: In the second step, you will be required to write an automated test script in the following manner

@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);
    }
}

Third Step: This step requires implementing functional code that is quite similar to that of the TDD. Take a look:

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();
}
}
}

Fourth Step: You’ve to run the said behaviour to ensure that it is successful. If it becomes successful, you can go to the next step. Else, you’ll have to debug functional implementation then run it.

Fifth Step: In this step, you’d have to refactor the overall implementation (this is an optional step). The action comes into being if there is a need for refactoring the code in submit method for printing error messages as shown below:

//match username and password 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");
}

Sixth Step: It nvolves writing different behaviour by following the steps from 1 to 5 Steps giving rise to new behaviour.

TDD vs BDD: Key Difference Between TDD and BDD

TDD BDD
TDD stands for Test-driven development BDD stands for Behaviour-driven Development
TDD is a process that involves writing a test case BDD starts by compiling an entire scenario according to the expected behavior
It focuses entirely on how the implementation of functionality occurs. BDD, on the other hand, focuses entirely on an application’s behaviour for an end-user.
The test cases are often written using a programming language The scenarios are often deemed readable when put into contrast with TDD. Also, BDD is written using basic English language
Changes occurring in applications functions do impact test cases in Test-driven development. Changes in functionalities don’t have much impact on BDD test cases.
Collaboration is required among the developers In BDD collaboration takes place among stakeholders
It offers a better approach for projects that involves third-party tools and API The approach is better for projects which involve actions driven by users. Such as - Application systems, e-commerce websites, among others
Tools that support TDD are TestNG, JUnit, NUnit, among others. The tools which support BDD are Mspec, SpecFlow, Cucumber, among others
Tests taking place in TDD could be understood only by the people who have complete knowledge of the programming language. BDD tests could only be understood by literally anyone, even the ones without any knowledge of programming languages.
With TDD, there are fewer chances of incurring blood in the tests In TDD, it is very difficult to test bugs and track them.

Test writing occurs right ahead of coding and begins in both BDD and TDD. In driven developments, the writing tests aid in predicting the best course for development. The ultimate goal of both driven developments is to prevent cases from missing functionality and code. The methods are more concerned with locating bugs and focus more on documentation.

Related Article: Frequently Asked BDD Interview Questions

Writing Failing Feature Tests vs Writing Failing Tests

Both approaches depend entirely on writing a commendable failing test as everything picks up from this point forward. The difficulties in BDD do satisfy both the customer and the developer when it comes to operation. Nevertheless, in TDD, the tests are designed to only tend to the satisfaction of the developer and their codes.

The figure shown below represents how BDD does work right over TDD, which further implements a very distinctive approach. Technically, BDD and TDD aren’t exact opposites, but they aren’t much similar. BDD ultimately ensures that the applications’ use cases would work while providing quite a better level of confidence.

writing-failing-feature-tests-vs-writing-failing-tests

For instance, the development team might use BDD to bring higher levels of tests confirming the applications' behaviour. When implementing specifics, developers may choose to create specific unit tests by ensuring the strength of components as these components would be reused across any application.

Conclusion

Many tech experts would say that BDD is better than TDD and vice versa. But the ultimate and inescapable truth is BDD has originated from TDD that answers all of the shortfalls of Test-driven development. Amalgamating both BDD and TDD in businesses and organizations eliminates the shortfalls of both technologies. You can program BDD to support the core quality that a developer writes. In contrast, the other driver development will help the system's behaviour that any product owner already determines.

About Author

author
NameRaunaak
Author Bio

As a content writer and storyteller, Raunaak Mitra regards himself to be a prodigy in writing. He firmly believes that language is borderless, and anyone can write as long as they have a strong narrative capability and the ability to emote feelings into words. Authors like F. Scott Fitzgerald and R. K. Narayan have influenced him to keep the writing as simple as possible for anyone to understand. Other than being well-versed about technological trends like AI, Machine Learning, AR, VR, Gaming, Microprocessors, Cloud Computing, Industry 4.0, literally any technological advancement (old or new), he also shares a knack to curate fiction on sci-fi, satire, and thriller genres.