Is Unit Testing worth the effort?

Technology CommunityCategory: TestingIs Unit Testing worth the effort?
VietMX Staff asked 3 years ago
  • Unit Tests allows you to make big changes to code quickly. You know it works now because you’ve run the tests, when you make the changes you need to make, you need to get the tests working again. This saves hours.
  • TDD helps you to realise when to stop coding. Your tests give you confidence that you’ve done enough for now and can stop tweaking and move on to the next thing.
  • The tests and the code work together to achieve better code. Your code could be bad / buggy. Your TEST could be bad / buggy. In TDD you are banking on the chances of both being bad / buggy being low. Often it’s the test that needs fixing but that’s still a good outcome.
  • TDD helps with coding constipation. When faced with a large and daunting piece of work ahead writing the tests will get you moving quickly.
  • Unit Tests help you really understand the design of the code you are working on. Instead of writing code to do something, you are starting by outlining all the conditions you are subjecting the code to and what outputs you’d expect from that.
  • Unit Tests give you instant visual feedback, we all like the feeling of all those green lights when we’ve done. It’s very satisfying. It’s also much easier to pick up where you left off after an interruption because you can see where you got to – that next red light that needs fixing.
  • Contrary to popular belief unit testing does not mean writing twice as much code, or coding slower. It’s faster and more robust than coding without tests once you’ve got the hang of it. Test code itself is usually relatively trivial and doesn’t add a big overhead to what you’re doing. This is one you’ll only believe when you’re doing it 🙂
  • I think it was Fowler who said: “Imperfect tests, run frequently, are much better than perfect tests that are never written at all”. I interpret this as giving me permission to write tests where I think they’ll be most useful even if the rest of my code coverage is woefully incomplete.
  • Good unit tests can help document and define what something is supposed to do
  • Unit tests help with code re-use. Migrate both your code and your tests to your new project. Tweak the code till the tests run again.
1 Answers
VietMX Staff answered 3 years ago

Unit testing is a lot like going to the gym. You know it is good for you, all the arguments make sense, so you start working out. There’s an initial rush, which is great, but after a few days you start to wonder if it is worth the trouble. You’re taking an hour out of your day to change your clothes and run on a hamster wheel and you’re not sure you’re really gaining anything other than sore legs and arms.
Then, after maybe one or two weeks, just as the soreness is going away, a Big Deadline begins approaching. You need to spend every waking hour trying to get “useful” work done, so you cut out extraneous stuff, like going to the gym. You fall out of the habit, and by the time Big Deadline is over, you’re back to square one. If you manage to make it back to the gym at all, you feel just as sore as you were the first time you went.
You do some reading, to see if you’re doing something wrong. You begin feel a little bit of irrational spite toward all the fit, happy people extolling the virtues of exercise. You realize that you don’t have a lot in common. They don’t have to drive 15 minutes out of the way to go to the gym; there is one in their building. They don’t have to argue with anybody about the benefits of exercise; it is just something everybody does and accepts as important. When a Big Deadline approaches, they aren’t told that exercise is unnecessary any more than your boss would ask you to stop eating.
So, to answer your question, Unit Testing is usually worth the effort, but the amount of effort required isn’t going to be the same for everybody. Unit Testing may require an enormous amount of effort if you are dealing with spaghetti code base in a company that doesn’t actually value code quality. (A lot of managers will sing Unit Testing’s praises, but that doesn’t mean they will stick up for it when it matters.)
If you are trying to introduce Unit Testing into your work and are not seeing all the sunshine and rainbows that you have been led to expect, don’t blame yourself. You might need to find a new job to really make Unit Testing work for you.