Behavior Driven and Test Driven Development
Behavior Driven-Development (BDD) focuses on the “outside-in” perspective, meaning the tests will test the behaviors which are related to business outcomes. The tests are on the behaviors i,e, business driven use cases.
Test-Driven Development (TDD) focuses on the “inside-out” perspective, meaning the tests create from a developer’s perspective. The tests are on the implementation logics and that is the driving factor behind Test-Driven Development (TDD).
Best Principles
- Isolation
Each test should run in isolated state , this means also that one test case should not impact other test case’s state.
2. Short and Simple
Each test should be very short and simple. It should run in very less time(i.e. in milliseconds)
3. Add test cases to build process
Always add the test cases to build process.
1. Get the code coverage matrix
2. Make sure that we are not breaking the existing system while implementing some new stuff there
4. Repeatable
Always produce the same results while running the same set of test cases.
Why TDD?
Test driven development uses for the code implementation tests and there are no logical errors into the code block.
The TDD are mostly targeted for small chunk of code at a single test considering to test one single aspect per test case. It will impact one layer and one class’s method at a time.
The advantages are as follows
1. It will make sure the code implemented correctly at each layer
2. Provide the code coverage per class
Cons
1. TDD will not guarantee the bugs on the system behavior
2. To represent a single aspect of system behavior , there are multiple test cases involves and those are spread across the different test classes per layer.
Why BDD?
Behavior driven development uses for the code tested from behavior aspects , i . e.
the business use case for the aspect has been developed correctly or not.
It could impact the multiple layers per test case depends on the business use case.
For example. In case the application having api layer, business layer , cache layer , data access layer , then a single BDD test case might impact all the layers.
The advantages are as follows
1. It will make sure the system behavior as per business use cases
2. One single test case provides code coverage from all the layers
3. It will help to reduce the bugs on system behaviors including its implementations
Cons
1. Developing the behavior driven test sometimes takes time than TDD
2. All the code paths are might not covered using BDD test cases
Example :
Let’s consider that an API application includes the following layers.
1. API layer
2. Business Layer
3. Data Access Layer
4. Cache Layer
Let’s consider that API application deals with Products and features.
And the business use case is as follows.
Retrieve all the products which has features like fraud detection.
Then the test case for BDD will look like as below.
@Test
Public void GetProducts_WithFeatureAsFraudDetection_ValiadateResponse()
{
// Arrange
// Act
// call to products GET api layer with proper request parameters and this will go through all // the layers as above, and consider to mock on the external endpoints
// Assert all the values in place for this test
}
Now, in case of TDD , the above use case will spread across the multiple test classes.
Common facts
1. Use of IoC [I.e Inversion of Control] , DI [ Dependency Injection] are absolute important to make the code testable
2. TDD and BDD both should follow the best principles for software testing , like isolation, fast and repeatable etc.
3. BDD is an extended version of TDD , means it will also includes system behavior tests along with unit test cases.
Happy reading.