Acceptance Testing with Cucumber

After watching a Pivotal Labs Tech Talk Making a Case for Cucumber, I decided to give it a try. Especially the seamless integration with rails and webrat made me curious. Webrat is a headless browser simulator, which can execute UI tests for you. You even can use the same syntax to drive a real browser using Selenium if you need Javascript support for some of your tests.

Features

For testing your application, you specify features consisting of scenarios. Each scenario is defined by a set of steps and validations. Here’s a short example:

Feature Contact Form
  In order to tell us something
  users
  want to contact us online without having to leave their browser

  Scenario: Submitting with valid infos
    Given I am on the contact page
    When I fill in "Your Name" with "Matthias Marschall"
    And I fill in "Your Message" with "What's up?"
    And I press "Send"
    Then I should see "Thanks for your feedback!"

Usually, one would need to implement fixtures to bind steps to the application code. But cucumber comes with a very useful set of pre-defined steps for webrat, in fact, all of the above steps are already there. You should be able to successfully execute the above example out of the box (given your app already has the contact page up and running).

Full Cycle E-Mail Testing with rails and cucumber

Filling out the contact form should send an email to a feedback account with the information provided. There exists a very handy plugin for dealing with email verification in a rails test environment: rspec-email

After installing it, you can add steps like these to the scenario shown above:

    And "contact@example.com" should receive 1 email

    When "contact@example.com" opens the email with subject "Feedback"
    Then I should see "What's up" in the email

    When I click the first link in the email
    Then ...

Again, all of the above steps are provided by the plugin (after calling script/generate….).

Without writing a single line of fixture code, I was already able to do a full integration test including form validations and email sending. Not too bad for 2 hrs worth of research. My next steps will be to try out the selenium integration and play around with setting up data in the database for driving my tests. Stay tuned by subscribing to our RSS feed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.