Inspired by a conversation at the office couple of days ago with a slightly bald software developer from the UK and @Stubbs I decided to play a bit with the concept of autotesting. I came across it about a year a go during BTDevCon and I really liked it, but back then I have seen it in action, when one of the developers was using it for a demo in Ruby. I haven’t heard about anything similar for PHP so I didn’t dig in further. Until now.
Let’s get back to the concept of autotesting again for a minute. The other term for it is continuous testing and in short it is about getting an instant feedback on your code while you’re developing it. In practise, the test suite is being triggered every single time you save a file and the results of the run are displayed on the screen. You don’t have to run the test suite manually, it is done for you.
All you need is watchr, growl and growlnotify (which is available as part of Growl Extras). Watchr is a flexible tool that allows you to watch the filesystem and run an arbitrary command when a change is detected, growl is a notification system for Mac OS X and growlnotify is a command-line tool to post Growl notifications.
Watchr
To install watchr you can either install it using gem:
#> sudo gem install watchr Successfully installed watchr-0.7 1 gem installed Installing ri documentation for watchr-0.7... Installing RDoc documentation for watchr-0.7...
or clone the latest version from github, generate the gem and install it from your local disk:
#> git clone git://github.com/mynyml/watchr.git #> cd watchr #> gem build watchr.gemspec Successfully built RubyGem Name: watchr Version: 0.7 File: watchr-0.7.gem #> sudo gem install watchr-0.7.gem Successfully installed watchr-0.7 1 gem installed Installing ri documentation for watchr-0.7... Installing RDoc documentation for watchr-0.7...
Growl and growlnotify
Growl (with included extras) is available from its home page as a standard dmg image. Just follow onscreen installation instructions.
Setup watchr with PHPUnit
To set up watchr with PHPUnit you only need one configuration file. In there, you define which part of the filesystem you want to watch for changes and what do you want to do when the change occurs. In my case all I want is to execute my test suites. So here is the example file, called watchr.rb and stored in the top level directory of my module:
In the first line, I have set up watchr to watch any file with php extension for incoming changes and when it detects the change to execute code_changed() function. code_changed() does nothing else but executes phpunit command in my Test subdirectory and passes the results to growl() function. And the growl() function basically parses the results, checks whether tests run successfully or not and executes growlnotify with an appropriate message and image.
Let the fun begin!
All you have to do now is run watchr, start changing your code and watch for growl notifications!
#> watchr watchr.rb
You can get both the configuration and images I have used from my github. Happy continuous testing!
Sources:
This is neat thanks 🙂
Can’t seem to get this to work. I can install watchr with no issues and the script runs as shown above but it does not seem to detect any changes so phpunit never fires. Any ideas?
It’s hard to say. The first thing I would check would be php error log. The whole thing won’t work if phpunit crashes.