TDD - Test Driven Development is a software development approach in which test cases are developed to specify and validate what the code will do. In TDD the tests are not done at the end of the software development, the tests are done before writing any production code.
- Is your code difficult to test?
- Don't you know if you have 100% test coverage on the project?
- Are you afraid to make changes to the project because you don't know where it will go wrong?
TDD solves these and other problems because with the laws of tdd every single line of code that you write will be tested.
1.You are not allowed to write any production code until you have first written a failing unit test;
2.You are not allowed to write more of a unit test than is sufficient to fail and not compiling is failing;
3.You are not allowed to write more production code that is sufficient to pass currently failing unit test.
You begin by writing a small portion of unit test. But within a few seconds you must mention the name of some class or function you have not written yet, thus causing the unit test to fail to complete. So you must write production code that makes the test compile. But you cant write any more code than that, so you start writing more unit test code.
Round and round the cycle you go. Adding a bit to the test code. Adding a bit to the production code. The two code streams grow simultaneously into complementary components. The tests fit the production code like an antibody fits an antigen.
TDD allows us to be more productive since the project only grows “if I can say so” after all the tests have passed.
Once the TDD is embraced it will be a step towards the Clean Code.
In the TDD scenario developers need to be very disciplined because sometimes when the calendar starts to get tight some tests may start to be ignored and if you haven't tested the code how will you know there is a hidden bug ?.