Member-only story

The three rules of TDD

Rabin Poudyal
2 min readMar 6, 2021

According to Uncle bob, there are three rules that should be followed to get the best results from TDD. They are:

  • You should write only enough tests that are sufficient to fail
  • You should only write enough code that is sufficient to pass the written failing code
  • Write production code only to pass the failing tests.

So did that sound a bit strange to you? Maybe you have been doing it all wrong since the start. For example, you might be writing all the tests ahead of time and then start writing the production code. Or you might be writing code earlier and write test later. That clearly violates the above three TDD principles and does not give the benefits of practicing the TDD approach. If you write all tests at first, here are something that can happen:

  • You will be exhausted by a bunch of failing tests
  • You might need to restructure the tests in the middle of writing production code,
  • You violate the Red Green principle which states the red green cycle should be quick,
  • You will accidently mix tested code and untested code. etc

The benefits that TDD brings are many. It does not gurantee you to write clean code but in most of the time it will. You might still write a bad code or a bad test but if done in a correct way it might save you hours of debugging in future.

If you like my article, don’t forget to follow me on Medium, or connect me on Linkedin, or follow me on twitter.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Rabin Poudyal
Rabin Poudyal

Written by Rabin Poudyal

Software Engineer, Data Science Practitioner. Say "Hi!" via email: rabinpoudyal1995@gmail.com or visit my website https://rabinpoudyal.com.np

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations