Testing WordPress with Cucumber
I should probably introduce myself first: I’m Mallory, Dextrous Web employee #1.
We do quite a bit of work with WordPress, and one of the problems we’ve found is that it doesn’t lend itself to the kind of automated testing that we do with the rest of our projects. The code for themes easily gets pretty cluttered, and it’s hard to know what the consequences of installing a WordPress upgrade or new plugin might be.
One of the tools that we use for our Ruby on Rails projects is Cucumber, a Behaviour Driven Development tool that makes front-end testing of Rails applications really easy. There’s an example below, but in brief: it lets you write human-readable tests and then run them on your application to make sure it’s working properly. Because Cucumber tests the front-ends of web applications, it’s not tied to any one technology — so we thought, why not use it to test WordPress?
So I spent some time writing a little bit of configuration magicks and step definitions, and here it is: cucumber-wordpress.
To get started you’ll want to install the gem:
gem install cucumber-wordpress --source http://gemcutter.org
Then copy the example features directory from the gem into the root of a WordPress installation:
cp -R /usr/lib/ruby/gems/1.8/gems/cucumber-wordpress-1.0/examples/features .
Configure your test database, and where the site is being served from:
Here’s a sample:
Background: Given WordPress is installed Scenario: Submitting a post Given I am logged in as "admin" And I am on admin dashboard When I follow "Add New" within "#menu-posts" And I fill in "title" with "I &lt;3 cucumber" ...
I’ve been testing a plugin with this for a week now, and it’s been very smooth.
Here at The Dextrous Web we’re committed to backporting the Rails culture to WordPress.