Tuesday, June 15, 2010

Write Unit Tests

That way, you can program like a monkey until the unit tests pass.

10 comments:

Robert Love said...

So do you feel like a monkey lately?

Chris Bensen said...

You make that sound like it's a bad thing?

angeltveit said...

If you put a million monkeys at a million keyboards, one of them will eventually write a Delphi program.

The rest of them will write Perl programs.

Warren said...

How can you tell, looking at a unit test if it is a useful unit test? One that will some day perhaps catch a regression? Hmm. I find that most unit tests I can think up are either (a) pointless, will never fail, (b) do not cover even 0.001% of the decision points or branches in a major function, and are thus shallow and (c) are often things that if I want to fix point (a) and (b) will become 500+ line behemoth unit tests.

:-)

W

Chris Bensen said...

Warren,

Like anything it really depends. There are always tradeoffs. First you have the tradeoff between writing a more badass program vs increasing the quality. Unit tests take time but in the long run will pay off dividends especially when paired with continuous integration with something like Cruise Control or Hudson.

If your functions are so large that they can't be tested consider breaking them up into smaller more testable functions. That will reduce the branches and allow you to create a table of all the cases. The cases may still be huge, but you really care about boundary conditions. Consider a function with the following prototype:

function Foo(Value: Integer): Boolean;

What boundary conditions would you use?

mb said...

Experience shows that the most useful unit tests emerge from former bug reports :)

LDS said...

"That way, you can program like a monkey until the unit tests pass."
Guess that explain a lot about Delphi lately

Chris Bensen said...

mb, Yes, bug reports turned into unit tests are great sources of tests. They allow an incremental addition testing a boundary case and they exercise a real world use.

Chris Bensen said...

LDS, if you mean the consistency of quality has gone way up then you are spot on! I wish I could shed some light on how we are using continuous integration around here and how many tests get run every day, on every checkin, every build, every OS permutation, etc, etc.

masonwheeler said...

Since nobody else has said it yet:

Code Monkey get up, get coffee,
Code Monkey go to job...

Post a Comment