I recently came across a quote attributable to Robert C. Martin which stated three rules for test-driven development. I'd like to share them with you:
- You are not allowed to write any production code unless it is to make a failing unit test pass.
- You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
- You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
I think that these are excellent rules that all programmers should consider strongly when developing code. I've waffled back-and-forth over the past few years on writing tests first, then writing code first and testing it later, and every other combination in-between. I'm not a fan of writing the complete test up front because at times I'm not really sure what the full functionality is going to be, and after all, isn't that why we design while we code?
I think that these rules properly state the way that test-driven development is supposed to work.