Effective Java Testing

December 10, 2008

Weasel Testing

Filed under: Process — esnickell @ 9:00 am
Tags: , ,

Ping pong pair programming is great.

You can get nearly the same effect, even if you have to develop solo. (But it won’t be near as much fun.)

The key to doing this effectively is that you need to have two personas. I’ve tried to come up with a good name for this technique, but so far I’ve only come up with “schizophrenic programming” or “devil’s advocate programming” or “stubborn testing” or even “two hats programming”. For now, let’s call it “weasel testing”.

You wear two hats, and you alternate between them. One hat belongs to the Good Tester. He’s only allowed to edit the test code. He’s always trying to force the Bad Programmer to write the correct production code. While you’re playing the Good Tester, your job is to write the next incremental piece of an excellent test. And your job is finished — for the moment — as soon as you have a correct but failing test.

Once the tests are failing, you change hats, and Weasel takes over. The Weasel isn’t a bad programmer. “Lazy” or “Contrarian” might describe Weasel better. Wearing Weasel’s hat, your job is to make all the tests pass by writing the simplest piece of code that satisfies the test. The simplest. No matter how stupid or wrong. Be stubborn. That Good Tester is trying to make you write the correct code, and you even know what code he’s trying to force you to write. But you’ll show him! You’ll think of some cockeyed but simple way to weasel out of his trap, and make the tests go green. Thereby showing the Good Tester that his tests aren’t up to snuff yet. Once the tests go green, it’s the Good Tester’s turn again. And you cycle back and forth until the Good Tester finally paints Weasel into a corner from which he can’t escape. At that point, you have a solid chunk of production code with full test coverage.

Do not underestimate how hard this is to do well. Every time you do TDD, you already pretty much know what code you’re trying to force yourself to write. This makes it very easy to write the “final, correct” code, even without sufficient test pressure. As a result, when you transition from Good Tester to Weasel, it’s worth pausing a moment, shifting contexts, and trying to think carefully about how to cheat and still get the tests to go green.

The better you get at playing Weasel, the better your tests will become.

1 Comment »

  1. [...] @ 9:00 am Tags: color commentary, p3, tdd, weasel I guess my head’s still thinking about weasel testing and ping ping programming. And [...]

    Pingback by Testing as Titration « Effective Java Testing — December 12, 2008 @ 9:00 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.