Unit Testing Series #2

Abhishek Pathak
3 min readOct 13, 2022

--

In this article you will learn a clear understanding of TDD & BDD approach. let us first understand what do they mean individually and how are they used.

If you didn’t gone over the testing triangle and you are android developer, please check my last article of basics of Unit Testing.

Test Driven Development

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.

TDD step by step -

Step 1: (Write a test case) Based on the requirements, write an automated test case.

Step 2: Run the test cases and watch test fails

Step 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.

Step 4: Run the test cases again

Step 5: (Refactor your code) This is an optional step. However, it’s important to refactor your code to make it more readable and reusable.

Step 6: Repeat step 1 to step 5 cycle for other test cases.

Behavior Driven Development

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.

BDD step by step -

Step 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.

Step 2 : (Write the automated scripts) This simple English like language is then converted into programming tests.

Step 3: (Implement the functional code) The functional code underlying the behavior is then implemented.

Step 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.

Step 5: (Refactor or organize code) Refactor or organize your code to make it more readable and re-usable.

Step 6: Repeat the steps 1–5 for new behavior.

TDD vs BDD

Let’s take a closer look at BDD vs TDD

Conclusion

Choosing between TDD Vs BDD can be 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. I personally prefer TDD.

Unit testing implementation series

Thanks for reading this article. Be sure to click 👏 below to applause this article if you found it helpful. It means a lot to me.

--

--