TDD vs BDD
Test Driven Development và Behavior Driven Development
Last updated
Test Driven Development và Behavior Driven Development
Last updated
Test Driven Development is a combination of Test First Development and Refactoring, which is a core part of Extreme Program (XP). The goal of TDD is to help source code ensure quality, be neat, and run smoothly. TDD appeared not to replace traditional testing methods, but to ensure the implementation of Unit testing (a method used to test the implementation logic of block units). individual code) to achieve maximum efficiency. Currently, TDD is a widely used method, in companies that use Agile methods - Flexible development model.
TDD is applied in the following steps:
Prepare scripts for software testing plans.
Write a test for the function and ensure that the test fails.
Rewrite the test enough to pass the test.
Optimize the code of the function just written, remove duplicate code if any, ensure the test still passes.
Repeat the above steps for the following functions.
According to Javier Saldana, to use TDD, you need to understand the following two principles :
Only write enough Unit tests to fail.
Only write enough Production code so that a failed test becomes a pass.
The TDD cycle can be encapsulated by three keywords: Red - Green - Refactor
Red/red: Write a failed test, and run it to see what error it encounters.
Green/green: Rewrite the code in the Red section to complete the test.
Refactor: Optimize code, test architecture, eliminate duplicate code.
Increase code accuracy, minimize duplicate error problems later.
Optimize the testing process, limit wasted effort (when only using enough necessary unit tests and no need for manual testing).
Understand the operating logic of the code.
Making scripts is laborious and time-consuming (affecting overall product development time).
Need to maintain regular testing.
Performing testing requires a high level of skill and experience, otherwise it will be easy to get carried away and do the wrong steps.
BDD is simply understood as an extension of TDD, developing behavior combined with general TDD techniques and principles. However, TDD focuses on testing code and internal architectural processes. BDD focuses on testing the behavior and functionality of software. It can be said that TDD tests every small code detail in the software, while BDD tests general behavioral functions. Normally, BDD is written in Plain Text Language called Gherkin, then saved as an Extension file called .feature.
According to Redwerk's Maria Filina, BDD is applied in the following steps:
Write scripts on Gherkin for behavioral test plans.
Run tests according to the script and check the operations of the software system
Identify situations and behaviors when following the script.
Examine each situation and behavior to determine if there are errors.
Identify and adjust the functions and behaviors necessary to complete the test.
Run the test again. If there are errors, repeat the above steps.
Provides an intuitive, overall view of the software.
Because it is implemented in the minimalist Plain Text Language, everyone, even without expertise, can still understand it, and customers can also know whether the developer is on the right track or not. From there, the developer knows exactly what the customer needs.
Because it tests general software behavior, instead of testing complex code, BDD can objectively see which functions are necessary and unnecessary for customers.
Because BDD is further developed through TDD, to implement BDD, you also need to have general knowledge of the TDD method to grasp the rules and concepts of software testing in the most scientific way.
There is no independent testing like TDD, but when applying BDD, there needs to be a close connection between the software developer and the .feature file writer. Because if behavioral testers do not understand the ideas of software developers, they will not be able to test objectively and reasonably.
Being easy to communicate and understand customer requirements is an advantage, but also a disadvantage that makes the behavioral testing process can become time-consuming and laborious if you have to follow each request from necessary or unnecessary for customers