In the first of three blog posts about TDD, I discussed why TDD is important.
In this post, I’ll highlight five behaviours that will make your TDD much more effective.
1. Write the test first
Obvious? Yes. Easy? No. But it’s really important to make sure your test can fail (is red) — if it can’t fail, then there’s no point in writing it, because it’s vacuous (won’t tell you anything). Don’t you dare write a single line of code before having a failing test!
2. Solve one error at a time
It’s tempting to jump ahead when you know what the next two steps should be. But get in the habit of being rigorously methodical about your test-solving. If you have a no-method error, define the method but don’t let it do anything. Let the error messages guide you.
Again — obvious but difficult to do.
3. Run your tests before committing to GitHub
You never know what fun thing has broken and it’s awfully embarrassing to push code up with failing tests.
4. Run your tests after pulling from GitHub
In case your colleague or you in a previous life has forgotten step 3.
5. Don’t refactor until you have a green test.
This is a hard discipline, but it will save you hours. Trust me. I spent a couple hours in a red/refactor/red/red/red/red/red/red/red/red/red. Total waste of time. Never again.