Search code examples
tdd

Why we should start with a failing test in TDD?


I have started practicing TDD in one of my play project. It is really interesting. However I do not understand, why we should start with a failing test? Is it because it's a rule and we have to follow that, or is there any philosophy behind this? Please share your ideas.

Regards, Rajib


Solution

  • It is to prove that the test itself is in fact doing its job.

    If the test passes before you've written or changed any code then clearly the test is not very effective, so write the test, ensure that it fails, then write the code to satisfy the test.

    Really, with TDD, each piece of code you write should be to fix a failing test. This way you ensure that your code is fully tested.