My personal take on TDD
Graham Cox Jan 26 '17
I'm going to start out saying something that will upset people. I don't personally think TDD works well for Unit Testing.
Now, don't get me wrong, TDD works fantastically for some people. And I personally find it works really well at a larger scale. But when it comes to writing Unit Tests, I find that writing the tests first just doesn't work as well.
This might be a side effect of my Java background (static typing, compiled language means that you can't write unit tests really until you've got something to test)
When it comes to Acceptance Testing however, I think that TDD is a great tool.
So, what's the difference?
At the Unit Testing level, you are generally not writing a single unit of code in one go. You will often find that you are writing several different pieces of code that all work together, and the design of these different pieces is fairly fluid whilst you are developing them. Writing the tests for these pieces of code is - for me - counterintuitive.
On the other hand, at the Acceptance Testing level you hopefully have a decent idea of what you are writing and how it is going to work. Whether this is the UI/UX, or the REST API, or whatever the external interface to all of this is, you should know how this part of it works before the tests are written. You should therefore know what you are testing and how it will work ahead of development, which means that you can write the tests for it first.
So, for me personally, TDD doesn't work well for Unit Testing.