TDD Part 2: Making your tests run on their own

resource testing tutorial
autotests-400-200

Post Tags

Growl notification for automated testing

In an effort to get better about testing when developing, I’m walking my way through the Nuttuts series of tutorials on test driven PHP.

In the last post, I installed PHPUnit and Selenium for automated browser testing.

In this post, I’ll look at setting up a system so that the tests will automatically be fired whenever work is saved; success or failure will be reported via Growl notifications.

I’ll be following this tutorial: Nettuts: Automatic Testing for TDD with PHP

In addition to the summary, here are the key bits I’ve added:

  • Additional details on how to install Gems
  • Cleaned up the watchr script and commented what it’s doing
  • Addressed how to implement Growl notifications, including installing ‘growlnotify’ and adding icons

Set up your project

Create a folder on your localhost and bring in the files from the zip Nettuts provides.

You should end up with a structure like this:

practice/
    /Classes
        /Nettuts.php
    /Tests
        /NettutsTest.php
    /autotest_watchr.rb

I suggest replacing the autotest_watchr.rb that comes with that package with the one I’ve shared here: https://gist.github.com/4335092

The version I provide is cleaned up, commented, and specific to Growl.

Install watchr gem

At its heart, Watchr basically watches any (or all!) of your project’s files, then executes arbitrary Ruby code of your choice when things change. -rubyinside

We’ll be using a gem (packaged Ruby application or library) to “watch” for changes in our files. This is just for watching…our projects we’re testing will be written in PHP.

Your mac should have Ruby installed, so we just need to install the gem.

Find out where your gems should live

$ gem env

Of everything it returns, what we’re interested in is this:

INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8

Now change into that directory and then install watchr

$ cd /Library/Ruby/Gems/1.8/gems
$ sudo gem install watchr

Test that watchr installed ok

$ watchr
Usage: watchr [opts] path/to/script 

Install Growl Notify

In the Nettuts tutorial it says “replace the notify-send command with the appropriate Growl alternative”… this one sentence was a bit of a wormhole since I had never worked with Growl notifications before.

First thing I figured out was that in addition to having Growl on my system, I needed something called “growlnotify”. Apparently it comes installed in the latest versions of Growl, but I didn’t want to upgrade so I dug around online to find it. Here’s a copy you can download:

growlnotify.pkg.zip

Download, unzip, run growlnotify.pkg to install.

Once that’s done, you can play around with Growl notifications. Run this in terminal and you should see a nice little Growl dialog.

$ growlnotify -m "Hello World"

2012-12-19_0328

For more details, here’s the manual for growl notify: http://growl.info/extras.php#growlnotify

Setting up icons for Growl notifications

This is optional, but if you want to make your notifications more visually clear, you’ll to need include pass / fail icons. Here’s two I created that you can use. Save them somewhere locally and note the location.

fail

pass

Let’s try another hello world, this time with icons

$ growlnotify --image "/path/to/your/icons/pass.png" -m "Hello World, with images"

Growl notifications using growlnotify

After you confirm you’ve got the right path for your images open autotest_watchr.rb and look for this line…You’ll want to replace my path with yours:

images_dir ='/Users/Susan/Sites/autotest/images/'

Run the watchr

Finally, let’s test out our watcher script!

$ watchr /path/to/file/autotest_watchr.rb

That should leave your terminal in a state of constantly running because it’s waiting for file changes.

Make any file change to Nettuts.php or NettutsTest.php and save it…because watchr is listening, you should see a Growl notification and also feedback in the terminal window.

Set up an alias for the watchr

Last thing, let’s make an alias so we can quickly fire up our watchr.

Open up your .bash_profile:

$ edit ~/.bash_profile

Add this line and save:

alias autotest="watchr /Users/Susan/Sites/tuts/automated/autotest_watchr.rb"

Restart terminal and test out your alias

$ autotest
Running autotest_watchr.rb

Comments are closed.