Tools

The Importance of Unit Testing

by on March 17, 2014 8:57 am

I’m currently working on an enhancement to an existing insurance premium rating algorithm. Anyone who has done work in insurance (whether it be in IT or on the business side) is probably well aware of how complex this can become.

Consequently, any significant enhancement to such a system is quite likely to be fraught with pitfalls and setbacks, and that’s even before considering the level and quality of business requirements and testing strategies. However, the existence of unit tests can help to minimize these risks.

Both unit tests that exist before development enhancements and unit tests you create as part of the development effort can be beneficial.

Existing unit tests benefit developers in at least two ways:

1. They could be considered a form of requirements as to how the system currently operates. Unit tests provide concrete expressions of business requirements. For those who understand concepts better through examples (such as me), these existing tests can be invaluable. Even with well-written business requirements, sometimes understanding can only come through examples such as this. It also provides the developer with a debugging opportunity for deeper understanding of the existing code, should this be found to be necessary.

2. They help to ensure that changes made by the enhancement do not break the existing system, or gives the developer a testing pathway, should the existing behavior need to be changed due to the enhancement.

New tests also provide their own benefits:

1. It should be obvious that they help to ensure that code meets requirements.

2. They also help to ensure that all logic conditions are accounted for. They force the developer to revisit the code they have just written and to approach it from different directions. This helps to root out conditions the developer might not otherwise think of, resulting in them to then alter the code to properly handle these conditions.

Sufficient Coverage

So, what are some ways we can ensure that we write unit tests with sufficient coverage? Below is a short list of some guidelines to follow:

1. How many tests are enough? Nothing that can break should be left untested, even seemingly trivial ones.

2. Most modern development environments have tools that will expose code that does not get executed during the run of your unit test suite. Use these tools to ensure you get the most thorough test coverage you can.

3.  Test boundary conditions, especially when testing something as complex as the algorithm I referred to in my introduction.

4. Bugs tend to collect within the vicinity of each other. If one bug is found, it is good practice to thoroughly test the code around it, since it is likely other bugs will exist there as well.

5. Lastly, and this can sometimes be difficult, tests must be as fast as you can possibly make them. When time gets short, it will be the longest running tests that get sacrificed first. As an example, I’ve had unit tests in the past that have processed over xml document provided as the result of a service call. These documents were huge, containing the data for entire policies, most of which we never even needed. To cut down on processing time, we edited these documents to contain only the data relevant to the tests being run.

Unfortunately, the decision to create unit tests or not isn’t always up to the developer. As I previously stated, I think most of those involved understand the importance on a theoretical level, but just are not willing to sacrifice the time up front to ensure that unit tests are written. However, as developers, we must try to incorporate them when we can.

— Robert Rice, asktheteam@keyholesoftware.com

  • Share:

Leave a Reply

Things Twitter is Talking About
  • In Part 2 of our series on creating your own #Java annotations, learn about processing them with the Reflection API - http://t.co/E1lr3RmjI7
    September 19, 2014 at 12:15 PM
  • The life of a Keyhole consultant - A Delicate Balance: It’s What We Do http://t.co/ToRpWY3aix Blog as true today as the day it was written.
    September 19, 2014 at 9:50 AM
  • 7 Things You Can Do to Become a Better Developer - http://t.co/llPNMUN8nQ
    September 19, 2014 at 8:43 AM
  • .@jessitron Good luck, you'll do great! Our team really enjoyed your KCDC14 talks.
    September 18, 2014 at 10:19 AM
  • RT @woodwardjd: 7 deadly sins of programming. I think I did all of this last week. #strangeloop http://t.co/f7QFq1SpqW
    September 18, 2014 at 10:03 AM
  • In Part 2 of our series on creating your own #Java annotations, learn about processing them with the Reflection API - http://t.co/E1lr3RmjI7
    September 17, 2014 at 3:18 PM
  • We send out our free monthly tech newsletter tomorrow - dev tips/articles via email. Not on the list? Sign up: http://t.co/h8kpjn419s
    September 16, 2014 at 2:58 PM
  • Want to chuckle? If programming languages were vehicles -http://t.co/quqHsUFCtR #funny
    September 16, 2014 at 11:41 AM
  • In Part 2 of our series on creating your own annotations, learn about processing #Java annotations using Reflection: http://t.co/DJZvQuarkc
    September 16, 2014 at 9:06 AM
  • Don't miss @jhackett01's newest post on the Keyhole blog - Processing #Java Annotations Using Reflection: http://t.co/E1lr3RmjI7
    September 15, 2014 at 12:02 PM
  • We're pretty excited - Keyhole's #BikeMS team raised 158% of their fundraising goal to benefit @MidAmericaMS. Plus, they had a great ride!
    September 15, 2014 at 10:38 AM
  • A huge welcome to David Kelly (@rheomatic) who officially joins the Keyhole team today! :-)
    September 15, 2014 at 10:00 AM
  • Sending warm thoughts to @eastlack, @cdesalvo, @wdpitt & all participating in #BikeMS this AM. Thanks for helping in the fight against MS!
    September 13, 2014 at 8:10 AM
  • .@rheomatic We are so excited to have you joining the team! Welcome :-)
    September 12, 2014 at 4:11 PM
  • As the official holiday is a Saturday, we're celebrating today! Happy (early) #ProgrammersDay to you! http://t.co/1CvUfrzytE
    September 12, 2014 at 1:55 PM
  • Tomorrow @cdesalvo, @eastlack, & @wdpitt are riding #BikeMS to benefit @MidAmericaMS. You can get involved, too - http://t.co/9boQwEUxth
    September 12, 2014 at 11:00 AM
  • RT @AgileDevs: 5 tips for great code reviews http://t.co/9PdbtEv0z8
    September 11, 2014 at 2:53 PM
  • The BEMs of Structuring #CSS - http://t.co/159suYtfx6 A quick introduction to the Block Element Modifier methodology.
    September 10, 2014 at 2:49 PM
  • A huge welcome to Joseph Post (@jsphpst) who has joined the Keyhole team this week!
    September 10, 2014 at 9:52 AM
  • @TheGrisExplores Absolutely, and thanks for the compliment! Here's an article that you might find helpful, too - http://t.co/7oxpaohCS1
    September 9, 2014 at 2:22 PM
Keyhole Software
8900 State Line Road, Suite 455
Leawood, KS 66206
ph: 877-521-7769
© 2014 Keyhole Software, LLC. All rights reserved.