I was taking a look at ZenTest a while ago, especially the autotest functionality. As stated in the ZenTest site, autotest:

  • Improves feedback by running tests continuously.
  • Continually runs tests based on files you’ve changed.
  • Get feedback as soon as you save. Keeps you in your editor allowing you to get stuff done faster.

To add more bang to your buck, you can get continuous notifications from growl every time you save a file, alerting you if any of your previous test cases have failed.

Googling around I wanted to see if this can be tied into testing with CakePHP. If I did a horrible job googling and there is already something out there that mimics this functionality please send me a link. Anyways, I stumbled across Watchr. Watchr is a tool that monitors a directory tree, and triggers a user defined action whenever an observed file is modified. At that point trigger a CakePHP test suite command line argument and send the results to growl. And there we have it, continuous testing with growl notifications on CakePHP using Watchr.

Step 1: Install the Watchr gem: http://github.com/mynyml/watchr

Step 2: Upload images that growl will use to display passed/failed cases

Step 3: Set up a watchr configuration file in app/config/watchr.rb

Step 4: Every time you develop have watchr runing. Type the following in your root directory:

watchr app/config/watchr.rb

End Result:



Failed and Passed images to pass to growl

Create a directory ~/.watchr_images and upload the following images or any image of your choice for failed and passed test cases:

Break down of the watchr.rb configuration file

Define a global variable that points to your cake console script. I wanted to load this variable from my aliases in .profile but couldn’t figure out how (my ruby skills suck):

$cake = "/Applications/MAMP/bin/php5/bin/php /Applications/MAMP/htdocs/yourProject/cake/console/cake.php"

Define a function that will parse out the details you want to pass to growl. I only cared about the number of passes and fails.

def growl(message)
  message = message.split('---------------------------------------------------------------')[3].split('Time taken by tests')[0]
  growlnotify = `which growlnotify`.chomp
  image = message.include?('fails') ? "~/.watchr_images/failed.png" : "~/.watchr_images/passed.png"
  options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}'"
  system %(#{growlnotify} #{options} &)

Define a function that will run a command-line program and passes the result to growl

def run(cmd) 
  result = `#{cmd}`
  growl result rescue nil
  puts result

Define a function that calls the test case of the pertaining model or controller that just got saved:

def test_changed_model_or_controller(file)
  type = file.split('/')[1]
  name = file.split('/')[2].split('.')[0]
  run "#{$cake} testsuite app case #{type}/#{name}" 

Define a function that calls the test case of the pertaining test case that just got saved:

def test_changed_test_case(file)
  type = file.split('/')[3]
  name = file.split('/')[4].split('.')[0]
  run "#{$cake} testsuite app case #{type}/#{name}"

Define a function that runs the entire test suite for changes in config files, app_controller and app_model:

def test_app()
  run "#{$cake} testsuite app all"

The heart of the script, the watchers. Here we state which files to monitor using regular expression pattern matching paths.

watch('app/(models|controllers)/(.*).php')  { |m| test_changed_model_or_controller(m[0]) }
watch('app/tests/cases/(models|controllers)/(.*).test.php')  { |m| test_changed_test_case(m[0]) }
watch('app/config/(.*).php') { |m| test_app() }
watch('app/(app_controller|app_model).php')  { |m| test_app() }

That’s it. Run the watcher via “watchr app/config/watchr.rb” as you develop and your test cases will continuously run giving you growl update notifications.

Download the watchr.rb config file

Watchr Readme
Watchr – Most of my time was spent here modifying this script and applying it to a CakePHP scenario
Setting up Watchr and Rails
Watchr: A Flexible, Generic Alternative to AutoTest – This was the first article I stumbled across that got me started on Watchr
CakePHP – Running Tests in the Command Line