Posts Tagged ‘Testing’

Using watchr with PHPUnit

December 16, 2011 Leave a comment

I’ve known about watchr and autotest for a while but have never taken the time to set them up.  I should have done this a long time ago, I noticed the efficiency gains immediately.

Initial Setup

Create a local Gemfile and install the necessary gems.

jheth@box1:~$ cd ~/project/src/test/
jheth@box1:~/project/src/test$ cat - > Gemfile << EOF
source :rubygems
gem 'watchr'
gem 'rev'  # Provides event support for Linux
jheth@box1:~/project/src/test$ bundle install --path=vendor --binstubs

This will install the watchr gem into a local vendor folder and create ~/project/src/test/bin which will contain the watchr executable script.

Configure Script

Next, configure the watchr.rb script.  You can configure any number of watches for files and/or folders.  The watch() function takes a regex with any number of captures to grab filename or parts of the path. The ‘do’ block is executed whenever a file changes. You can change the phpunit parameters to whatever you want.

# Watch test files
watch("src/test/model/(.*\.php)") do |md|
   system("cd src/test/model && phpunit --debug --colors '#{md[1]}'")
# Watch src objects
watch("src/main/model/(.*)\.php") do |md|
   system("cd src/test/model && phpunit --debug --colors '#{md[1]}Test.php'")

Notice that these paths are RELATIVE, which means you must run this script from ~/project.  I had trouble with both absolute and relative (../) style paths so I just decided to run it from a higher level folder.

cd ~/project && ./src/test/bin/watchr ./src/test/watchr.rb

To see what files are being watched you can use the -l argument. This only prints the names, it doesn’t actually begin watching.

./src/test/bin/watchr -l ./src/test/watchr.rb

Fire this up in a separate terminal and let it just sit there all day while you edit your files. You will see the phpunit output everytime you save a file. I turned on –colors and –debug so I don’t have to guess when something goes wrong.

Wrap Up

This has already saved me a ton of time since I would previously switch between terminal sessions, typing or arrowing to the phpunit command I wanted to run. Now I never have to leave the editor and when saving either the test or the underlying source file, the tests get run. Decreasing the amount of time between edits and test execution is a huge time saver. I get instant feedback every time I save.

The startup command is a bit annoying to type so I added an alias to my .bash_profile.  No matter where I am, I can just pick a terminal and type ‘watchem’ and I’m up and running.

alias watchem='cd ~/project/ && ./src/test/bin/watchr ./src/test/watchr.rb'
Categories: Uncategorized Tags: , ,

Testing your PHP Code

September 19, 2011 Leave a comment

Depending on which circles you run in, hearing PHP will either make you cringe or bring you joy and comfort.   Another recently blogged on this topic and I really liked what he had to say.

Since he argued that point, I’m here to point out some of the testing libraries that are available for those of you using PHP.  You can see from the list below that PHP is gaining ground in an area many feel to be lacking.

In no particular order:

Provides unit testing, basic web functional testing, and a mock API.

I’ve been using PHPUnit for over 3 years now and it’s constantly improving.  It has strong community support and is backed by Sebastian Bergmann.   It provides unit testing, a mocking API, functional web testing via selenium,  database testing (DBUnit), code coverage generation utilizing xdebug, and a number of output formats easily consumable by various tools (My favorite: Jenkins).

A pretty new framework (May 2010) that promotes ease of use. It makes heavy use of a Fluent interface for writing tests.

It delivers itself in the form of a PHAR (PHP Archive) file and is run like this:

php mageekguy.atoum.phar –testIt

Easy to install via PEAR and integrates with PHPUnit as an alternative to the Mock API PHPUnit provides.

Not to be confused with phake, which is a rake/make equivalent for PHP, which is also what phing provides to the PHP community….

Capital P, Phake, is yet another mocking API built to overcome some of the short falls in other mock APIs.  mlively is a heavy contributor to PHPUnit’s DBUnit module and has written this module.

You guessed it, Ruby has RSpec and PHP has PHPSpec.  A BDD testing style framework.  I’ll let you read more about it.

Ruby has Cucumber, PHP now has Behat.  Behat was inspired by and tries to stay true to Ruby’s Cucumber approach.  I really like the “feature file” approach at this level of testing.  How easy is it to read and collaborate on test scenarios when they’re in plain English?

Example taken from their website.

Feature: ls
  In order to see the directory structure
  As a UNIX user
  I need to be able to list the current directory's contents

  Scenario: List 2 files in a directory
    Given I am in a directory "test"
    And I have a file named "foo"
    And I have a file named "bar"
    When I run "ls"
    Then I should get:

These feature files are backed by “step definitions” that carry out the actions described here.  By creating a DSL (Domain Specific Language) you could have almost anyone write and execute tests.

The makers of Behat also bring us Mink,  a web functional testing tool.   Supports both headless browser (HTTP request/response) and in-browser testing for validating functionality.  It provides driver abstractions that integrate with a number of other open source software to accomplish its goal.

Categories: Uncategorized Tags: ,