Think of unit tests as regression tests

Unit tests can and should be thought of as regression tests.  Regression testing provides the capability to determine if a change made in one part of a class or component introduces a bug in another part.  This means that the test is automated and can be run multiple times and always yield the same answer.

Each test should have the capability to be run separately.  This means that any setup required by a test be done by each and every test.  It is ok and recommended to use a setup function that checks to see if it was called before to do the setup and only do the setup once for a group of tests that need it.

I first became aware of the power of automated regression over 20 years ago while working at Litton Guidance and Control Systems on a Fortran IV compiler.  The customer wanted a detailed test of the capabilities of the compiler.  Back then, these kinds of tests were all being done manually.  I envisioned myself running this test and dying of boredom for hours on end.  So, I thought about it and realized that the VAX/VMS computer had the capability to redirect input to take it from a file instead of a terminal.  Likewise for output, the output to a screen could easily be redirected to a file.  So, I wrote my first automated test simply because I did not want to waste hours of my time manually running tests multiple times for my manager, his manager, and at least once for each customer representative.  After finishing the automatic test, my manager asked me to add a new feature to the compiler.

I added the new feature as requested and tested it.  It worked correctly.  For the hell of it, I decided to run the automatic tests one more time.  The tests showed some differences with the verification files that should not have been there.  After I looked into it more deeply, it turned out that the feature I had just added introduced a sophisticated bug in another part of the compiler.  I then spent a few hours and fixed the bug and re-ran the automatic tests, which passed successfully.  When I first wrote the tests, I mainly did it to save time and so that I would not have to be present to run the tests, which would take at least 4+ hours to run manually.

What I learned from this is that there is tremendous power in regression tests.  A well designed regression test provides the capability to quickly discover bugs introduced from one part of a  class or component into another.  This capability is invaluable to an organization that cares about the quality of its software.

Prev          Next          Back to write the unit tests

Copyright 2011 by Robert G. Bryan


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s