How to Encourage Unit Tests

Greg Wheeler
Posted by


 

Many industry experts agree that unit tests have been proven valuable and are worth completing thoroughly; however, adequate trials do require a significant amount of coding and developer time. As a result, some programmers—and software companies—downgrade the priority of unit tests, meaning that they are not completed if time or financial pressures arise. You too may believe that relegating unit tests to the back burner is a good plan; after all, your coding may be absolutely sound, and in the short term, not running unit tests will save both money and time. However, if you choose to forgo unit tests, the long-term consequences may be extremely expensive and tedious.

 

Unit tests are very precise analyses of each and every one of the software components in your application. In layman's terms, an application is the digital equivalent of large Lego model; individual software units would then be represented by Lego blocks. Naturally, the Lego block arrangements inside the model would vary depending on your overall structure, and applications will usually have a number of different levels and functions.

 

If your application is particularly complex—the digital equivalent of a Lego block onion patch, for example—unit testing it will also be more complex. As you examine your three-dimensional programming puzzle, every layer of code in each of the digital "onions" needs to be stripped away and tested.

 

This process may sound extremely time consuming: that's because it is.

 

However, the advantages of unit testing are significant. If you ensure that your code functions as intended before releasing your application, you significantly cut the risk of programming inconsistencies, and fewer glitches means happier end users.

 

According to Agile product design and development coach Lasse Koskela, the principles of writing effective unit tests remain the same regardless of the software's programming language. Some of the best practices for unit testing that apply to all applications:

 

  • Ensure that your code is solid before unit tests commence. Well-written code is worth the investment, particularly if it stops you needing to write a slew of additional drivers and stubs at test time.

     
  • Never use one unit test to analyze another unit test. Create a separate driver and stub for each test type and keep your process consistent.

     
  • Use inversion of control to determine your code's unit testing suitability before you start.

     
  • Test behaviors rather than methods. The end goal of testing is to ensure that each piece of code behaves as it ought to in relation to every other piece of code it interacts with.

     
  • If your application is very complex and you want to dispense with some of the more minor unit tests, always dispense with the simplest code-to-code relationships first. This is called the "Too Simple to Break" rule.

     
  • Name your unit tests appropriately so that other programmers know what they actually do.

     
  • If possible, incorporate testing tools into your build processes so that your code execution and tests are double-checked.

 

Tackling a bug-filled application may not be your cup of technological tea, but if you choose not to conduct unit tests, you may find yourself pulling all-night emergency coding sessions. If you can detect a problem with a base-level coding element during unit tests or via testing tools, you can correct it before moving on to elements at the next level. Problems can then be eliminated before they become mysterious user-level glitches. Starting with a user-level anomaly, on the other hand, is a little bit like stepping into a labyrinth without a map. The right piece of code, or code file relationship, is often a complete enigma: a coding needle—or even worse, more than one coding needle—in a complex programming haystack.

 

Because of the time it takes programmers to write drivers and stubs—and the consequent financial investment—some companies reduce the priority of unit testing. In many cases, these coding tasks are given to less experienced developers or they end up not being completed at all. However, if you plan to release a functional, reliable application, the advantages of unit tests are undeniable. If you invest a little money in useful analyses, you might avoid the major post-release application anomalies that are so ubiquitous and are often notoriously tough to diagnose and solve.

 

(Photo courtesy of Freedigitalphotos.net)

Comment

Become a member to take advantage of more features, like commenting and voting.

Jobs to Watch