Exploration Through Example

Example-driven development, Agile testing, context-driven testing, Agile programming, Ruby, and other things of interest to Brian Marick
191.8 167.2 186.2 183.6 184.0 183.2 184.6

Tue, 25 Feb 2003

Command lines as refactored tests

I did another test-first demo, this time in Ralph Johnson's software engineering course. I'm chagrined to say that in a 1.5 hour class, I ended up producing only this code in two test-then-code iterations:

 def move(seconds, from_id, to_id)
   shorten(from_id, seconds)
   shorten(to_id, -seconds)
   [records.record_with_id(from_id), records.record_with_id(to_id)]

We were just starting to get into the swing of things when I looked at the clock and saw I had five minutes to go. Argh!

Part of the slowness was due to popping up for meta-discussions about test-first (and, I confess, digressions like the implications of test-first for the choice between statically and dynamically typed languages), and part of it is that I had to explain the application under test. But, as I was writing this note, I found myself chastising myself for not being dutiful enough about refactoring common test code.

You see, the move command of the Session object moves time between two time records. That means my test had to install records before calling move:

 first_record =
   @session.add_record(FinishedRecord.new(first_time, 1.hour,
 # do the move, check the results.
But to do that, my test had to create (and I had to explain) a Time and a Job to be part of the FinishedRecord.

So, I found myself telling myself, I should have long since factored out an add_record test utility that created the intermediate objects for me.

But wait... it's common for me to test new internal methods through the command-line interface. If I'd done that, my test would have looked like this:

 job 'first job'
 job 'second job'
 add_record '12:45 pm', 1.hour, 'first job'
 add_record '1 pm', 2.hours, 'second job'
 # do the move and check the results.
(Because my command line is Ruby, I can use the same unit testing tools as I do when testing internal objects.)

In other words, the command line 'move' is almost the easy-to-use-and-explain utility method I berated myself for not having written. It just lives at a different layer, one with a pretty straightforward translation to the Session.

I deviated from my common practice because I didn't want to have to explain the tradeoffs between testing methods directly and testing them indirectly. I wanted to launch into showing students some tests. Instead, I found myself explaining too much and slowing myself down.

I don't want to make any general claims for this technique. But for this application and this person, the command line serves as a handy collection of test utilities. Because the command line is Ruby, I can also "dive beneath the surface" and call methods directly when I need to.

I love command lines.

## Posted at 15:34 in category /testing [permalink] [top]

About Brian Marick
I consult mainly on Agile software development, with a special focus on how testing fits in.

Contact me here: marick@exampler.com.




Agile Testing Directions
Tests and examples
Technology-facing programmer support
Business-facing team support
Business-facing product critiques
Technology-facing product critiques
Testers on agile projects

Permalink to this list


Working your way out of the automated GUI testing tarpit
  1. Three ways of writing the same test
  2. A test should deduce its setup path
  3. Convert the suite one failure at a time
  4. You should be able to get to any page in one step
  5. Extract fast tests about single pages
  6. Link checking without clicking on links
  7. Workflow tests remain GUI tests
Permalink to this list


Design-Driven Test-Driven Design
Creating a test
Making it (barely) run
Views and presenters appear
Hooking up the real GUI


Popular Articles
A roadmap for testing on an agile project: When consulting on testing in Agile projects, I like to call this plan "what I'm biased toward."

Tacit knowledge: Experts often have no theory of their work. They simply perform skillfully.

Process and personality: Every article on methodology implicitly begins "Let's talk about me."


Related Weblogs

Wayne Allen
James Bach
Laurent Bossavit
William Caputo
Mike Clark
Rachel Davies
Esther Derby
Michael Feathers
Developer Testing
Chad Fowler
Martin Fowler
Alan Francis
Elisabeth Hendrickson
Grig Gheorghiu
Andy Hunt
Ben Hyde
Ron Jeffries
Jonathan Kohl
Dave Liebreich
Jeff Patton
Bret Pettichord
Hiring Johanna Rothman
Managing Johanna Rothman
Kevin Rutherford
Christian Sepulveda
James Shore
Jeff Sutherland
Pragmatic Dave Thomas
Glenn Vanderburg
Greg Vaughn
Eugene Wallingford
Jim Weirich


Where to Find Me

Software Practice Advancement


All of 2006
All of 2005
All of 2004
All of 2003



Agile Alliance Logo