Loving unit tests (was RE: [Mono-list] Re: IEnumerators)

Nelson Minar nelson@monkey.org
Tue, 17 Jul 2001 08:50:35 -0700 (PDT)

>~ j.  // I WILL learn to love writing unit tests.  I WILL learn to love
>writing unit tests.

Heh, I know how you feel. I found the whole idea distasteful until I
tried it. Now I love it, at least for library programming like what
Mono is doing.

Why? Because all along, when I was writing code, I'd be testing it.
But I'd test it ad hoc. Like I'd be writing a base 16 decoder in Java,
so I'd code up a main() method that took command line arguments and
try out a few. "Base16Decode 0fa8", yeah, that looks right. Etc. This
is known as "testing by poking around".

Then I tried unit tests. Instead of doing my little tests by hand, I
wrote them up as programs.
  assertEquals(Base16.decode("0fa8"), {0x0f, 0xa8});
The tests were the same, it only took a bit more time to write them.
But now I have those tests forever. Six months later, other
programmers could hack my decoder and trust that they hadn't broken it
because the tests still passed.

Good programmers always test their code while they're working on it.
Unit testing just lets you formalize those tests into something useful
for others.

The XP folks suggest writing tests before writing code. I find this
helpful, but difficult discipline. So instead I endeavour to write
unit tests instead of testing by poking around.

.       .      .     .    .   .  . . http://www.media.mit.edu/~nelson/