Software development is a process that we can use various method to approach, Test Driven Development(TDD) is one of them.
TDD founded by Kent Beck as a part of Extreme Programming at year 2003.
Fundamentally, TDD is the concept where we write our tests before start coding and refactor our code accordingly.
Let’s have a look at the cylce of TDD.
Add a test
Starting to development by creating test according to our needs and spesifications.
Run all tests.
At first, we should expect them to fail, it is the reason that we need new code to meet our needs.
Write simplest code to pass the tests
In here we just need to make our test case green, we can even make it hard-coded.
All test should be okay
If there is any failed test cases repeat all the steps above
Refactor the code
Now,Refactor the code to make it clean, simple, easy to maintainable, remove if any hardcoded exists.Think about how to improve your code.
Make sure your tests are running after all changes.
Repeat this cycle for all developments.
Advantages of TDD
- By writing the test cases first forces us to focus on what we want from the code.
- TDD creates a detailed specification.
- TDD reduces time spent on rework.
- We will spend less time in the debugger.
- We are able to identify the errors and problems quickly.
- We can easily tell if our last change broke the working code.
- TDD allows the design to evolve and adapt to your changing understanding of the problem.
- TTD helps simplification of the code. We will only write code in response to the requirements of the tests.
- TDD creates SOLID code.
- TDD supports a clean interface.
- TDD creates code that is maintainable, flexible, and easily extensible.
- The resulting unit tests are simple and act as documentation for the code. Since TDD use cases are written as tests, other programmers can view the tests as usage examples of how the code is intended to work.
- Development costs are cut.
- Quality is improved.
- Bugs are reduced.
- TDD will give us enough flexibility to integrate new functionality.
Limitations of TDD
Despite its many advantages, many developer teams find it difficult to use TDD efficently. Many of them having trouble where to start or what to do next, this may cause some developers to be too much detail-focused and losing the sense of big picture and business side of the product.
Some development teams also find it hard to maintain too much unit tests, as with or without TDD many unit tests are tightly coupled to implementation of the code, they focus on testing the method rather than what it should do.
TDD In Agile
Test Driven Development is one of the common practies of Agile.
In Agile, Feedback is Critical.
The requirements can change on the way. If you want to give your customer what they still want, you need; quick feedback.
Agile development is not about running fast… It is about running fast in the right direction at a sustainable pace.
And, TDD is a way to get quick feedback.
Thanks to “Test first” approach, gives developers good understanding of what code meant to do and guidance of user functions, that is why TDD naturally fit with Agile.
Taking requirements as a base to create test cases increase the communication between developer,tester and the owner of the requirements. This will help to understanding the work better.
By creating the test first, there is no waiting time to testing to be done. It can done immedialty as it already exists. If there is any problem occured, it can send back to backlog immediately.
As you evolve the system based on feedback, bug fixes, and additional features, it tells you that the code worked and continues to work as expected.