In this article, I'll try to prove that TDD (Test Driven Development) can be faster than just Unit Testing.
For clarity, by TDD I mean writing unit tests before the code. And I will refer to unit tests that are written after the code as just "unit tests" or "regular unit testing".
Note about accuracy of the information: I personally follow TDD on daily basis, and I have followed regular Unit Testing as well in the past, just before I matured into TDD. So, let's agree, I know what I'm talking about.
I won't go into details about the process behind TDD or Unit Testing, but I will provide visuals representing these processes. This will serve as a vocabulary afterwards.
As you can see, I tried to be consistent when naming steps of both these approaches. This shows there are similarities, and the number of steps is somewhat equal, too. But those steps mean slightly different things for each approach. That's where I want to dig to reveal the truth.
I'll pick each step from TDD and will compare it to the same step from regular unit testing process.
Let's swallow the frog and say that TDD loses right away with the first step.
While with regular unit testing you don't need to think too much and can jump right to coding, you can't do so with TDD. Thought process is slightly different, and requires more discipline and more skills. You need to do one of the following:
Regardless of which trick you use, it will take more time than it took for regular Unit Testing. The more advanced approach you take, the more time it will take to accomplish.
But don't judge too early. The more you practice TDD, the faster you will be handling this step in more advanced ways. Eventually, you'll start loving yourself for being so good at it.
Besides, while you can jump to coding right away with regular unit testing, who said that it's a good idea? It's like acting without thinking, so maybe TDD does not really lose at all...
Now, you need to write unit test. It's much easier to write unit test when you don't need to study the code which you are going to cover. There is no code - there is no headache. Just write unit test the way you want it to be. TDD catches up on this step quite rightfully.
With regular unit testing, you not only need to study the code that you need to cover (even though you probably just wrote it), but you also need to fix the code from time to time to make it unit testable. There are always some small things that you cannot think of when you are writing code - while being so excited that you don't want to bother with thinking about unit tests. And one more issue - now the code is written, so you also need to fight the procrastination and be bored to death to write the unit test for already working code.
That's straightforward with TDD. You just need to stop your temptation from writing too much, but that's not too difficult, because at this point it has become almost like a game with yourself.
With regular unit testing, you are writing code in about the same way. Except - you probably write more code than you need to (again, developer's excitement, or better - anxiety). Not only did you introduce some potentially dead code (which real use cases never needed), but you also need to cover that with unit tests later on - so yes, you just added overhead to unit testing as well.
So, on this step, TDD wins.
Both processes require about the same effort to run unit tests for verification purposes, but regular unit testing should lose a bit here. That's because you will need to keep adjusting unit tests to make them pass finally. Changing the working code is the last thing you want to do at this point. If you do change the working code, then you have to ensure it still works now, so no way.
With TDD, the test is already written, the way it should be. You just expect it to pass now that the code is written.
Final step, and the biggest advantage of TDD - running program to verify the code correctness.
With TDD, you probably have to run 1-2 times. And in most cases, it will work fine, because the unit test has already passed as a verification of that exact code.
With regular unit testing, on the other hand, you are doing this exercise while writing code. You can either write too much code and then debug every single step and keep fixing it, or you can write small piece at a time and run it. Either way, running, debugging, and fixing is much slower than just executing unit tests.
So in this step again, TDD takes over.
If you read all this and kept open minded, and if you have not started practicing TDD yet, it's about time now.
Unit testing, TDD, and other Test-First approaches are some of the great ways to increase the development skills and get into the next level of engineering. I can help with this - I provide a training course about Test-First Development practices for groups.
The author of the above content is Tengiz Tutisani.
If you agree with the provided thoughts and want to learn more, here are a couple of suggestions: