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
  • New to #JavaScript prototypal inheritance? Here are some notes to help you along the way - http://t.co/NTIDZS6Uhy
    July 5, 2015 at 7:55 AM
  • ICYMI: we've released a demo version of #GrokOla which is open to the public. Try out its features & capabilities - http://t.co/O4ladowmFU
    July 4, 2015 at 3:05 PM
  • Happy 4th of July from the Keyhole team! We hope that you have a happy and safe holiday with your family and friends.
    July 4, 2015 at 9:55 AM
  • Let's talk testing. Here are common challenges #Agile teams face when writing automated tests & how to overcome them: http://t.co/DrKbNZJcE0
    July 3, 2015 at 11:06 AM
  • #GrokOla users get free educational tutorials. But lucky you, we've released some to the public. #JavaScript primer - http://t.co/nIR9XiWY6O
    July 3, 2015 at 10:55 AM
  • Being able to isolate debugging techniques can help make you a better debugger. Here's Time-Oriented #Debugging http://t.co/UplJgP4VzC
    July 2, 2015 at 10:50 AM
  • RT @zachagardner: @zachagardner has declared it is @ChipotleTweets day at @KeyholeSoftware . You have been warned 🐓🐖🐄
    July 2, 2015 at 10:09 AM
  • Current state of random number generation & the differences in how #Java & #JavaScript approach it - http://t.co/5tBKNXnu8T #security
    July 1, 2015 at 2:45 PM
  • Woohoo - 600 followers! Thanks, everyone. We'd love to ask you - what type of tweets / dev content would you like to see more of from us?
    July 1, 2015 at 10:38 AM
  • We would like to welcome Dallas Monson to the team today! Dallas is a Senior Architect focused on UI/UX and #JavaScript. Welcome, Dallas!
    July 1, 2015 at 8:35 AM
  • Good introduction to TypeScript - http://t.co/0N22fVpAHt Plus, how to approach modularization in #TypeScript - http://t.co/wxRWGBj3Uh
    June 30, 2015 at 3:25 PM
  • .@mrbristopher just delivered a new S911 Night Drone to James Hayes, winner of our #kcdc15 giveaway! Congrats, James! http://t.co/RriJIxubH2
    June 30, 2015 at 11:35 AM
  • It feels like primitives could have been left out of the initial implementation of #Java. See why - http://t.co/A8ChCBHXJO
    June 29, 2015 at 4:05 PM
  • Developers in a bounce house! I repeat, developers in a bounce house! We had a blast at our 1st company picnic. Pics: http://t.co/XIqs7ECUst
    June 29, 2015 at 1:40 PM
  • New #SpringBatch tutorial from @jhackett01: Spring Batch – Replacing XML Job Configuration With JavaConfig http://t.co/PmdXnriKQu #java
    June 29, 2015 at 11:46 AM
  • We had such a fun time at the Keyhole company picnic! Pictures to come, including some of our developers in the bounce house. #loveourteam
    June 29, 2015 at 8:41 AM
  • In #JavaScript, how do we harness the power of callbacks without the confusing mess of nested functions? Promises - http://t.co/j1gAJ9hi3D
    June 29, 2015 at 8:40 AM
  • .@zachagardner We are so happy that your family attended! This will definitely need to be repeated every year!
    June 28, 2015 at 8:14 PM
  • Thank you to all on the Keyhole team who came to our first inaugural company picnic! Wonderful food, family and bounce house fun!
    June 28, 2015 at 7:50 PM
  • Debugging is a challenging part of being a programmer. We have a tutorial series to help, with a #JavaScript focus - http://t.co/rfhjJo64P7
    June 27, 2015 at 1:45 PM