John Sammis

Unit Testing – A Key Part of Web Development Best Practices

March 10th, 2011

Unit Testing: What is it and Why is it Important?

A unit test is a program that verifies that another program works as expected. Unit testing is an approach to software development in which the programmers write tests for their code to make sure that it continues to work as the system evolves.

A simple example of a unit test for some programming code that calculates sales tax based on the state in which an order is placed would have inputs of a state and an order amount, and test whether the result is the expected sales tax.

The ‘test suite’ is run continuously during development, instantly alerting developers to emerging code issues. The developers also run the test suite before publishing updated code to make sure they don’t break anything that used to work. Some software development environments, such as Rails and .NET provide unit testing frameworks out-of-the-box. Other development environments need varying levels of work to configure a working unit testing framework.

A common objection to unit testing is the time and expense it takes to write the tests themselves versus the time it would take along the way to fix bugs as they emerge. We at GORGES have found this to be a spurious argument. As we write programs, we have to check that they work and that they don’t break other programs. If we don’t have tests, we have to manually click around. That introduces both the element of human error (oops I forgot about that!) and the expense of paying humans the time it takes to do all that clicking when a machine could do it just fine. However these are not the only benefits of unit testing:

  • Programming code changes that break an existing test are identified immediately, allowing the programmer to fix the problem while the issues are still fresh, saving time, and money.
  • Unit tests provide effective code documentation so that new programmers on the project can become productive faster.
  • Changes made to supporting software, such as frameworks and operating systems, are tested quickly, reducing the risk of introducing bugs when technology upgrades are made.
  • The cost of implementing new features in the future is reduced because the unit tests provide context and ensure that old programming code is not broken by the new programming code
  • Refactoring, a common task of restructuring the code to make it easier to use and maintain, easier and less time-consuming because the unit tests ensure the refactoring process does not break the application.
  • Regression bugs, issues that appear multiple times, can have specific unit tests written for them, so ensure we don’t get ‘bitten’ twice or more times by the same bug.

Unit testing is not a panacea for ensuring that a software application is free of bugs – almost all software applications exhibit a problem at some point. The value of unit testing is that it helps to significantly reduce bugs, and make it easier and cheaper to fix any bugs that do appear.

Tags: , , , , , ,

Leave a Reply

©2012 GORGES - All rights reserved
where programming meets design and lives happily ever after