Thursday, May 22, 2014

What's a Unit Test?

Well, what's a, "Unit"?

A unit is a sub-component of a modular architecture.  Wikipedia defines a software unit as: "...the smallest testable part of an application.".  (http://en.wikipedia.org/wiki/Unit_testing)

If a car engine is just a single object and all of its components are fabricated (built) together, it's not modular.  That is, if you can't build the components separately there are no sub-units.  You're left with the ineffective and costly testing method of system testing the entire engine.  But you can't effectively test an alternator by driving the car.

Car manufacturers get this.  They design automobiles to be modular.  This means they can design, build, and test individual components and they can do this in parallel.  It also means individual components can be repaired or replaced in the field.  Car manufacturers often outsource unit production.  They can do this because they have a modular design.  They design the required interfaces and behaviors of the sub-components and provide those specifications as requirements to the sub-component's manufacturers.  Those specifications inform the sub-component manufacturers' unit test planning, including test jig design.  (A test jig is an instrumented tool that connects to and exercises a sub-component's interfaces.)  Unit testing is done before the sub-component is sent to the car manufacturer to be bolted into the car.  It's the sub-component manufacturers' responsibility to assure their product's quality.

Take the example of a car's alternator.  Chevrolet provides specifications to ACDelco.  Those specifications include the overall dimensions, the size and positioning of the mount points, the diameter of the belt sheeve, the electrical output at different speeds, heat output, the electrical cable dimensions and connections, and so on...  ACDelco creates unit tests to assure their alternators meet Chevy's specifications.  They build a test jig based on the specifications.  They use the jig to execute unit tests before shipping their alternators to Chevy.  Notice that although Chevy may do further testing, it is ACDelco's responsibility to test their product.  It's.  Their.  Product.

So modularity is a requirement for software unit testing.  If you can only build the monolithic application, you can't unit test.  A software architecture must allow modules to be individually built for them to be individually tested.  The developer is responsible for writing unit tests alongside product content.  This investment pays us back every time the unit tests find a bug.

Todd Shoenfelt
http://www.linkedin.com/in/toddshoenfelt

No comments:

Post a Comment