Why writing tests are worth it

Today I got myself thinking, what are the main benefits of system testing? We all think testing our code is important, but why? In this blog post I have collected a few points which in my opinion makes writing good test cases worth it. And of course, we are talking about automated tests.

  • Documentation – Good tests forms an excellent documentation on how to use and understand the code. How should you instantiate a class? How should you call that service? What are the limit values? The list here can be very long…
  • Improved code quality – I like to believe that good test provides at least some level of code quality. The fact that the coder bothered to write test is an indication on that he actually tried to do some quality work. Of course, if the tests sucks, they are not worth the bytes used to store them.
  • Verification of requirements – Without tests, how can you be sure that the code actually solves the issues it was supposed to solve? How do you now we are building the right thing?
  • Safer to refactor – With many tests of high quality I would have a better feeling refactoring code. In my opinion refactoring code is extremely important to ensure that we constantly improve the design of our solution all the time. I can’t imagine how to do this without tests (at least if I must refactor others code)
  • Instant feedback – The best feedback we get is from our users. Test though have the strength of giving us instant feedback while we develop. This instant feedback is important because it allows us to detect bugs earlier. The earlier we catch a bug, the less costly it is to fix it.
  • Limit values– Some application states are hard to reach with manual testing. With test we can just mock those services and instruct it to return those hard-to-reach corny values
The cost (time required) of fixing bugs rises if we discover them late in the development phase. The price is highest if the bug is discovered after the functionality is released. I usually visualize this in my head as the figure illustrated below. In my experience the cost of fixing a bug discovering in production is significantly higher, than if we manage to catch it during development.

I guess there are plenty more benefits of automated system testing. Please leave me a comment to let me know what I left out.

If you enjoyed this blog post, you would probably also like the one I did about TDD by example. TDD is a great way to make sure you are testing your code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s