Coding With Confidence
Once upon a time—maybe it was last Tuesday—there were
two developers, Pat and Dale. They were both up against
the same deadline, which was rapidly approaching. Pat was
pumping out code pretty fast; developing class after class and
method after method, stopping every so often to make sure
that the code would compile.
Pat kept up this pace right until the night before the deadline,
when it would be time to demonstrate all this code. Pat ran
the top-level program, but didn’t get any output at all. Nothing. Time to step through using the debugger. Hmm. That
can’t be right, thought Pat. There’s no way that this variable
could be zero by now. So Pat stepped back through the code,
trying to track down the history of this elusive problem.
It was getting late now. That bug was found and fixed, but Pat
found several more during the process. And still, there was
no output at all. Pat couldn’t understand why. It just didn’t
make any sense.
Dale, meanwhile, wasn’t churning out code nearly as fast.
Dale would write a new routine and a short test to go along
with it. Nothing fancy, just a simple test to see if the routine
just written actually did what it was supposed to do. It took a
little longer to think of the test, and write it, but Dale refused
to move on until the new routine could prove itself. Only then
would Dale move up and write the next routine that called it,
and so on.
Dale rarely used the debugger, if ever, and was somewhat puzzled at the picture of Pat, head in hands, muttering various
evil-sounding curses at the computer with wide, bloodshot
eyes staring at all those debugger windows.
The deadline came and went, and Pat didn’t make it. Dale’s
code was integrated and ran almost perfectly. One little glitch
came up, but it was pretty easy to see where the problem was.
Dale fixed it in just a few minutes.
Now comes the punch line: Dale and Pat are the same age,
and have roughly the same coding skills and mental prowess.
The only difference is that Dale believes very strongly in unit
testing, and tests every newly-crafted method before relying
on it or using it from other code.
Pat does not. Pat “knows” that the code should work as written, and doesn’t bother to try it until most of the code has
been written. But by then it’s too late, and it becomes very
hard to try to locate the source of bugs, or even determine
what’s working and what’s not.
What is Unit Testing?
A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code
being tested. Usually a unit test exercises some particular
method in a particular context. For example, you might add
a large value to a sorted list, then confirm that this value appears at the end of the list. Or you might delete a pattern of
characters from a string and then confirm that they are gone.
Unit tests are performed to prove that a piece of code does
what the developer thinks it should do.
The question remains open as to whether that’s the right thing
to do according to the customer or end-user: that’s what acceptance testing is for. We’re not really concerned with formal
validation and verification or correctness just yet. We’re really not even interested in performance testing at this point.
All we want to do is prove that code does what we intended,
and so we want to test very small, very isolated pieces of functionality. By building up confidence that the individual pieces
work as expected, we can then proceed to assemble and test
working systems.
After all, if we aren’t sure the code is doing what we think,
then any other forms of testing may just be a waste of time.
You still need other forms of testing, and perhaps much more
formal testing depending on your environment. But testing,
as with charity, begins at home.
Why Should I Bother with Unit Testing?
What Do I Want to Accomplish?
Does It Do What I Want?
Does It Do What I Want All of the Time?
Can I Depend On It?
Does it Document my Intent?
How Do I Do Unit Testing?
Excuses For Not Testing
……………………………… .
…………
etc all the question and to understand the testing, download the pdf below and read.