For the past few days I’ve been struggling with a moral dilemma: I’m attracted to the idea of test driven development (TDD) and writing unit tests before actual code, and it’s great whenever I do excercises to try it out. My problem is that although I’d like to work in a completely test driven way, I have problems actually practicing the methodology to the letter. So, “good that I have a blog”, I thought and decided to ask my readers (that’s you!) for help.
The ideal procedure presented in TDD literature and for example eXtreme Programming is that you start with a minimal piece of functionality that you want to add to your code, then you write a test for that. First the test of course fails because there is no code available. But once you’ve written enough code to make the test pass, you move on to the next test. And so on.
I get stuck at the first step. When I start a new application and need to write that first test for it, I just don’t know what to write. I don’t want to do big up-front design because it’s against the Agile principles, so how do I know what my first test should be?
My solution so far has been to start with some design and a prototype of some kind. For example a mock up of the UI, some screens laid out in HTML or even some Rails controllers. Slowly I start to see the application and how it should be designed, and then I write my first test. That’s when my prototype starts to evolve into a real application. One test at a time, I create the model class and move functionality where it should be instead of the place where I dumped it in the first place when I didn’t know what I should be doing.
This violates a few principles: first, some people say that you should only use your prototype during design and then start from scratch again. Instead of doing that I’m evolving my prototype into this fully functioning piece of software. Second, as I said, it’s not really test first: I’m doing kind of a coding phase before I know what I should start testing. And sometimes it’s not only in the beginning. And I’m not prototyping only at the beginning of the project: When I’m done with the clear part and again start to work on something I’m not sure how it should be done I go back to prototyping mode until things get clear again.
So, I have a few questions that I hope you could help me solve:
- Is it OK to go to a prototyping mindset every now and then and still think that you’re being test driven?
- How strictly are you, other developers, following practices like TDD? Do you think they should be practiced like a religion, or should we be using our common sense and create our own mixes of practices? Why?
- And if you’re able to do completely test driven development, what’s your secret? Where do you find the first test? And so on…
And now, the floor is all yours.