How to Start Using TDD
If you’re interested in how to finally start using TDD, I’ve got some ideas. In my experience it only takes two simple steps. It’s really easy to start TDD but a bit harder stick to it. But not that hard.
Let me show it in an example. Create something really small, maybe a console app that prints time in a different city or current temperature outside.
1. “One small step”
Create a test that is going to check just one thing and nothing more. The test will be failing at first and you’ll need to write code to fix it.
For example: your first test can just check that your application’s main file exists.
2. “One giant leap”
Keep writing code until your test starts passing. When it passes, stop, go back to Step 1 and repeat! Write another test that fails.
That’s it, really simple, isn’t it?
The good thing is that you don’t have to spend much time on each step, it takes a only few minutes (or even seconds) to write a test and then fix it.
What’s best in all this is that you see the feedback immediately. You’re going to like those green check marks next to your tests when they pass successfully.
Why it works
As with any new technique, you need to be consistent in repeating the steps until it finally sticks in your brain. But even after that you need to practice and master the skill every day.
There is no magical mantra or “do it 100 times and you’ll know it”, you have to keep doing it for as long as you can.
In time it’s going to be easier to do it than not to do it.
Here is your reward
Finally, the question “does this program really work?” will have a definite answer that you can be confident in. You’ll see that with TDD you get major benefits that no other technique can possibly give you:
TDD keeps you laser focused on the problem you’re trying to solve and ensures that you’re moving in the right direction.
Test code written once is executed million times and each time it rewards you with confidence in what you’re doing.
TDD forces you to write the best code you can and helps you to grow as a professional.
There is more but these are the best.