What is TDD?
TDD (Test-Driven Development) is a concept that (as the name says) focuses on test driven development, but what does that mean? It means that for every new feature, each new method, must have created a test before.
Among the benefits of testing, the main ones are:
- Reducing the number of bugs
- There’s much greater confidence that the code does what it should do.
- Elimination of useless code
- There’s tools that show which parts of the code aren’t being called by tests.
- Cleaner code
- The development cycle allows refactoring of the code, that, by being developed along with tests, tends to be simple.
- To write tests we have to separate the code into small pieces to make them testable, that is, our code will be less coupled.
TDD has a cycle called Red, Green, Refactor. In summary, it means:
- Red: We write a Test that initially fails¹
- Green: We make the Test pass
- Refactor: We refactor the code
¹ - Why do a test that doesn't pass? A: Because we want to know if the test isn't failing for configuration, if it's calling the right method or simply ensuring that it fails for the right reason.
But the complete flow is:
- We write a Test that initially fails (Red)
- We add a new feature of the system
- We make the Test pass (Green)
- We refactor the code of the new feature (Refactor)
- We write the next Test
Many people don’t like TDD to have more code to be developed (the tests), which can increase the time to develop a feature.
However, everyone knows that most of the effort and cost of a project is at the stage of maintenance. Many projects no longer exist because maintenance was impossible.
As seen with the benefits of TDD, maintainability (damn difficult word) of the project is high (becomes much easier to maintain). Therefore, the time spent on the test saves the headache you would have and the time you would lose later.
These are the main concepts of TDD. In future posts related, I want to show the tests in practice, but for this, these concepts need to be very solid in your head.
I hope you enjoyed.