Automate your Feature and Acceptance Tests in Four Easy Steps

Any significant web application at some point faces the question of how to approach testing of user-facing functionality. Developers could be (hopefully) happily churning out their JUnit-s, NUnit-s and Test::Unit-s, practicing TDD and Continuous Integration, but what does testing your classes, methods, and functions have to do with what your customers actually need from your system?

The answer is: not much, really. Testing web application from the end-user perspective involves dealing with test cases written in a business language, and secondly, firing multiple browsers and hunting down why this particular feature works perfectly fine in a browser Foo and does not work at all in a browser Baz. The right tools for automating this process are Behavior-Driven Development tools and browser “drivers”. These tools come from different development worlds and communities: Ruby, .NET, Java.

Fortunately, you can use many of these tools to test any web application regardless of your application platform whether it’s JavaEE, .NET, ColdFusion, PHP or Ruby on Rails. The tools came the long way since early days of Fit/Fitnesse and you can get from zero feature tests to automation in few easy steps using any desktop OS: Windows, Mac OS X or Linux. I promise.

What might be the most difficult part of this process is bewildering number of choices to make. A lot of choice is a Good Thing and all the tools mentioned in this post are free, open-source tools, but how it all fits together could be quite confusing. Fear not. Let’s walk through the steps.

Step 1. Select and Install a BDD Tool

Behavior-Driven Development is taking automated unit testing a level higher. The emphasis is on a language and readability of tests (or rather scenarios, broken down to steps in BDD parlance). One of the most active communities championing BDD tools is Ruby world. The framework that generated a lot of buzz in the last couple of years is Cucumber. It is a flexible Ruby framework that uses a wonderful domain-specific language for writing your scenarios: Gherkin. Gherkin is pretty much structured English or one of 37 other supported spoken languages. Cucumber natively uses Ruby for glue code (step definitions) that ties your features, scenarios and steps to browser drivers (or to native target applications).

Now, if you don’t know Ruby you might be tempted to go with one of Cucumber ports, for example cucumber-jvm (Java) or SpecFlow (.NET). You would get to write glue code in your programming language of choice, but the setup if often more complex, ports could be less mature or lagging behind flagship Ruby tools. It is hard to come up with other benefits of ports for testing web applications. Why not use this opportunity and learn a fun dynamic language with a lot of interesting ideas? It is used as the first language to teach kids, after all, and you don’t have to learn Rails and a lot of other Ruby frameworks, just the language itself.

My choice: Cucumber

Other choices: Fit/Fitnesse, SpecFlow, RSpec, easyb, etc.

What to do:

  • Windows: Install Ruby 1.8.x and add it to the PATH. Latest versions of Mac OS X come with Ruby 1.8.x. Don’t use Ruby 1.9.x just yet (see below).
  • Set RUBYOPT=-rubygems in your environment.
  • Go to your command line and install the Cucumber Ruby gem.
    >gem install cucumber

    Note: Any gem installation might fail with the following:
    ERROR: does not appear to be a repository
    This problem is transient. Try again in 5 minutes. Another option is to download needed .gem manually and install it:

    >cd {download directory}
    >gem install -lV cucumber

    The local installation (-l) will install Gem dependencies only if they are available in the Gem caches or in the local directory. You might need to download dependencies manually as well.

  • Windows: (optional, but highly recommended in order to display colored output) Install ANSICON.
  • Install RSpec for writing assertions in your glue code.
    >gem install rspec