Lithium Acceptance Tests using Cucumber and Webrat
Introduction
Cucumber (aka. cuke) and
webrat are ruby tools for behaviour driven development (BDD) and while they're mostly used on rails apps, they can be applied to PHP apps too. Our motivation for doing this comes from using a virtual machine (vm) for our (work) development. We're using
lithium now for all new projects, and we want to do BDD and Test Driven Development (TDD) as well. TDD is fine – lithium supports this really well out of the box (and
michaeltwofish is currently writing a lithium plugin to allow the command line testing to output to
hudson). Our plan was to use selenium for the acceptance tests (read BDD), but as selenium needs to run a firefox executable, this can't work (easily) on the vm. So we've now got webrat (and cuke) running instead.
Most of this post is taken from other sources. The first place to read is
aslakhellesoy/cucumber/wiki/php. It's also well worth looking at
chits: a real PHP application that uses cucumber/webrat.
Also see
Acceptance tests on a PHP project with the cucumber / webrat / selenium trio.
I repeated these procedures on
darwin and
centOS without any problems.
Installing ruby and gems
I downloaded and installed
ruby 1.9.2 from source. This also installed the latest version of ruby gems.
$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.5.0]
$ gem -v
1.3.7
Installing required gems (cucumber and webrat)
These are the gems I installed:
$ gem install cucumber
$ gem install webrat
$ gem install mechanize
$ gem install rspec
Layout
You'll need to put your cuke and webrat files somewhere. Typical location is the
app/tests/features directory. The feature files go in the top "features" directory:
+––– apps/tests/features/
+–––feature1.feature
+–––feature2.feature
+––– step_definitions/
+–––webrat_steps.rb
+–––support/
+–––env.rb
+–––hooks.rb
+–––paths.rb
Features
A feature could map to a high level user story (in the agile method), and be made up of a number of scenarios to be tested. The feature is executed by cucumber and the output will show whether your acceptance tests have passed or failed (or some other error has occurred).
There's plenty of information about how to write these feature files on the
cucumber site.
Step Definitions
Each step of a scenario (within the feature file) needs a definition in code of what is to be executed. These steps go in the
step_definitions/webrat_steps.rb file. Some of the steps defined here are "standard" eg:
Then /^I should see "(.*)"$/ do |text|
assert !!(response_body =~ /#{Regexp.escape text}/m), response_body
end
which is used to check if a particular piece of text appears on the page.
See the
webrat repo on github, as well as the
cucumber wiki page on step definitions, but also take a look at the
step definitions of the chits app as this has a lot more examples.
Support Files
There are three support files.
env.rb is used to setup the testing and code here is run before (and possibly after) every scenario. The
paths.rb file is a convenient place for the URLs webrat will access. The
hooks.rb isn't strictly necessary but if there is code that needs to run before (or after) specific scenarios, this is where it goes.
See the
wiki page on using cucumber for PHP apps and the
chits support files for examples.
Conclusion
In Part 2 I will talk about getting this to work on a lithium application, in Part 3 I will talk about using cucumber to drive selenium tests and in Part 4 I will talk about putting it under continuous integration with Hudson.