Jeff's Blog

Musings about software development, Java, OO, agile, life, whatever.


Wednesday, March 11, 2009 
Two Greens in a Row

I'm not refactoring, I'm test-driving new code, writing assertions first, yet I get two green bars in a row. Stop! I need to figure out what's going on. The first possibility is that I didn't expect to get green:

  • Did I compile? Am I picking up the right version of the code? - D'oh!
  • Does the test really specify what I think it does? - I take a bit of time to read through the test.
  • Do I really understand the code well enough? - I dig into the code, and in rare circumstances fire up the debugger. Maybe someone wasn't following YAGNI.
Alternatively, if I did expect to get green:
  • Did I take too large a step? - I consider restarting from the prior red, looking for a way to take more incremental steps. Obviously a more-granular increment of behavior exists, since I felt to compelled to write a distinct assertion for it.
  • Am I simply being completist? - I'm not testing, I'm doing TDD, where two greens in a row suggests that I don't need the second assertion. Test-driving is about incrementally growing the system, not ensuring that everything works. But from a confidence standpoint, I want to probe at interesting boundary conditions. Sometimes my compulsion to probe is because I don't understand the code as well as I should. Sometimes there's just too much going on, and I find that adding confidence tests is worthwhile. And finally, I remember that my tests should act as documentation. So most of the time, I'm ok with being a "completist."
  • Are there "linked" (i.e. redundant) concepts in the design? - Maybe the interface is overloaded, deliberately so. More often than not, I can link the two concepts in the test as well, building a custom assert; conceptually I end up with one assert per test. If I find that I have a lot of tests with more than one postcondition, my designs are probably getting less cohesive. Or maybe I'm just writing too broad of a test.

There are no doubt other reasons for two greens in a row. No matter, the event should always trigger a need to stop and think about why.


Comments:
I have found that I often make two dumb mistakes while tdd'ing. I am using Ruby's unit testing framework on a project, which is very similar to Junit 3.8. I often times forget to puts test_* as my test name and after writing what seems to be a failing test, I run my test and nothing....all green...wtf?!? I have spent 10 minutes looking at a test thinking...there is no way that can be green.

The other one I do a little less frequently, but still fall prey to it is using the wrong assertion. I have sometimes says "assert foo, bar" instead of "assert_equal foo, bar". The first will always pass as long as foo is an object.

There are a ton of these I'm sure.
 
Post a Comment

Links to this post:

Create a Link



<< Home

RSS Feed (XML)

Archives

February 2004   March 2004   May 2004   September 2004   October 2004   January 2005   February 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   June 2006   August 2006   January 2007   February 2007   March 2007   April 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   May 2008   June 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009  

This page is powered by Blogger. Isn't yours?