Faster TDD with Stakeout.rb
-
I’m a big fan of Autotest and it runs almost constantly on my machine. Autotest automatically reruns your tests whenever your files change. Instead of constantly flipping to another shell to rerun your tests, just let Autotest cheerfully do it for you in the background. It’s highly addictive.
The only problem with Autotest is that it is specific to Ruby. At work, I do a mix of different kinds of programming including Ruby, PHP, Python, and C. I’d like my TDD to be accelerated for all of these languages.
Thanks to Geoffrey Grossenbach, last week I came across stakeout.rb from Mike Clark. This is a tiny, dead simple Ruby script that runs an arbitrary command when certain files change. This is a stripped-down Autotest for everybody else. I’m sure it has all kinds of other uses as well.
To get started testing with
stakeout.rb, you’ll need Ruby installed. Any recent version is fine and you might already have it installed. Next, grab the stakeout.rb script and add the shebang line to the top (Unix-like OS assumed):#!/usr/bin/env ruby -w if ARGV.size < 2 puts "Usage: stakeout.rb <command> [files to watch]+" ...
Make the file executable and put it somewhere in your
PATH. You can test it out by typingstakeout.rbfrom an arbitrary directory and you should see the help message.Next, change over to a project directory where you have some test files. Most of the projects that I am involved with tend to use some directory structure similar to this:
/project_name /lib /test ...
To test such a project, run
stakeout.rbfrom the/project_namedirectory. Most PHP projects using PHPUnit tend to have anAllTests.phpfile or equivalent to run all the tests, so we’ll assume this for the example:project_name$ stakeout.rb "php test/AllTests.php" **/*
The first argument is what command to run when the tests change. The second argument, and any subsequent arguments, are the files to watch for changes. These can use a Ruby globbing pattern. The pattern
**/*will watch all files underproject_namerecursively, which includeslib/andtest/.Once
stakeout.rbis run, it will show no output but will sit and wait for changes. As soon as you change a watched file,stakeout.rbwill automatically rerun you tests and will continue to do so until you exit with Control-C.

6 comments
comment by Ryan Davis 5 Sep 07
now that’s just sad… Geoffrey didn’t have THAT in mind!
comment by Mike Naberezny 5 Sep 07
:-)
I’d like to see everyone do more testing, regardless of whatever language they happen to be stuck using.
comment by René Leonhardt 1 Feb 08
comment by Joaquin Lippincott 24 Sep 09
Here’s a post on using stakeout.rb with Drupal:
http://www.metaltoad.com/blog/fun-stakeoutrb
comment by Josh Ribakoff 18 Nov 09
I ported it to PHP, and started breaking up the code into objects and sub-routines. http://pastebin.org/54979
- override runTests() with the command you want to run
- override doDelay() to check more/less often
- pass the path to files in the contstructor
Invoke like this:
C:/wamp/bin/php/php5.3.0/php E:\dev\path\phpautotest.php E:\dev\path\to\monitor
pingback by PHP Auto Test – Automatically monitor your project for changes and run your unit tests « Web Application Development, E-commerce, Sales & Marketing 18 Nov 09
[...] read about stakeout.rb but read that it might not work on windows. Rather then waste time even trying I just ported it to [...]
Post a comment