I recently had a job interview with a enterprise company. The interviewer asked me about, what is difference between TDD and BDD? I answered him gently that both of them are system development approaches. TDD focuses mainly on testing the implementation whereas BDD focuses mainly on testing the behaviour of the feature. TDD is a development practice whereas BDD is a team methodology. After interview, I wanted to reverify that what I answered was correct. I googled, the question was surprised to see several blog posts answering the question in their own way. I don’t want to point out any blogs here, but I decided to point out my understanding in my blog. Hope, I will be able to convince you with my answer.
Test Driven Development, Behaviour Driven Development, Acceptance Test Driven Development
TDD3 is a software development technique. In this development technique, we create test cases1 first, and then we write the code underlying those test cases. Three important points to remember when talking about TDD are:
- It means writing tests that fail because the functionality does not exits.
- It is a development practice.
- Developers write the tests.
- Main focus: Unit testing
TDDFlowchart illustrating test driven development
TDD process is simple and straightforward. We start by writing a test. We execute the test and watch it fail. We modify the code and re-execute the code. If test passes, we can either stop or add new test. If test fails, we modify the code until, the test passes.
TDD methodology follows a simple 6 step process:
- Write a test case: Based on the requirements, write an automated test case.
- Run all the test cases: Run these automated test cases on the currently developed code.
- Develop the code for that test cases: If the test case fails, then, write the code to make that test-case work as expected.
- Run test cases again: Run the test cases again and check if all the test cases developed so far are implemented.
- Refactor your code: This is an optional step. However, it’s important to refactor your code to make it more readable and reusable.
- Repeat the steps 1- 5 for new test cases: Repeat the cycle for the other test cases until all the test cases are implemented.
Don’t be confused TDD or BDD with Domain Driven Design (DDD). Domain-driven design (DDD) is the concept that the structure and language of software code (class names, class methods, class variables) should match the business domain.
BDD is regarded as an extension of TDD where instead of writing test cases, we start writing executable specifications. Later, we write the code which is required to abide by these specifications. Four essential points you need to remember about BDD are as follows:
- It means writing executable specifications that fail because feature does not exits.
- It is team methodology.
- Executable specifications are written by developers, users or testers.
- Main focus: Understanding requirements
The process involved in BDD methodology consists of 6 steps and is similar to that of TDD.
- 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.
- Write the automated scripts: This simple English like language is then converted into programming tests.
- Implement the functional code: The functional code underlying the behavior is then implemented.
- 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.
- Refactor or organize code: Refactor or organize your code to make it more readable and re-usable.
- Repeat the steps 1-5 for new behavior: Repeat the steps to implement more behaviors in your application.
Context: Login page Event: Enter username and password and hit Login button. Outcome: I am logged into the user dashboard page.
- It is similar to BDD, however BDD focuses mainly on behaviour of a feature where as ATDD focuses on capturing the accurate requirement.
- It is a team methodology.
- Developers, customers, WAs
- Main focus: Writing acceptance tests
It is a development technique that focus on the implementation of a feature.
It is a development technique that focuses on the system's behavior.
It is development practice.
It is team methodology.
Developers, Customers, QAs
Reduce bugs in implementation
Reduce bugs in behavior of the system.
If you landed on this blog without understanding software design and modelling tools, you might want to stop it here and read this post first.
There is no right answer. Some developers might argue TDD is better than BDD, however, it depends on the person, the project, team lead and methodology. TDD gives you high code coverage 2 whereas BDD is better in finding the workflow bugs.
Test cases: In software engineering, a test case is a specification of the inputs, execution conditions, testing procedure, and expected results that define a single test to be executed to achieve a particular software testing objective, such as to exercise a particular program path or to verify compliance with a specific requirement. ↩
Code coverage: In computer science, test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs. ↩
TDD: Beck, Kent. Test-driven development: by example. Addison-Wesley Professional, 2003.