You know what TDD is and why you want to do it… but how do you actually do it? That part still escapes you. Red-Green-Refactor, yeah yeah yeah, you’ve heard it a million times before… but that still doesn’t help you with the HOW.
Believe it or not, understanding TDD is as simple as asking one simple question: “Does the software… X?”
Does the software…
- log the user in and show their name?
- send an email?
- save something to the database?
- produce a string in all capital letters?
Every single test you write is actually a question: does the software…X? And the answer to that question is whether or not the test passes.
How do you do TDD? You ask a question, that you know the answer to. You ask a question, where the answer is no. And then you change the software until the answer is yes. Then you ask your next question.
Try not to think about TDD in terms of testing (even though it has “test” in the name, and you write lots of tests!).
Think of it as if you were telling someone about a program you’re working on, and they started asking you questions: “Does the software…X?”
- If you have a passing test, you can say “Yes!”
- If you have a failing test, you can say “Not yet.”
- If you don’t have a test, you would say, “I’m not sure” (or maybe, “I think so… but I’m not sure”).
Getting started with TDD is really simple. Just ask a question. Turn your question into a test. Change the answer. Then, ask another question.