Code Free Acceptance and Integration Testing

In Ruby on Rails this is made possible through Cucumber, in PHP it’s possible using Behat + MinkExtension.

Behat is the integration testing framework, and Mink is the magic for easy code-free web-based acceptance test.

Wouldn’t it be nice if you could describe how the application was supposed to work in plain English.. and all the testing happened automagically? Well here you go:

Feature: Search
  In order to see a word definition
  As a website user
  I need to be able to search for a word

  Scenario: Searching for a page that does exist
    Given I am on /wiki/Main_Page
    When I fill in "search" with "Behavior Driven Development"
    And I press "searchButton"
    Then I should see "agile software development"

Run Behat with the MinkExtension installed and it performs all the magic: simulates navigating to the /wiki/Main-Page, entering text into a search field, pressing a button, and then scanning the page for a piece of text. Sounds awesome? Cool let’s set it up.

Step 1: Install Behat and MinkExtension

If you aren’t familiar with composer, follow the instructions from packagist. It will change your life. I’ll give you a minute and wait here until you have composer installed.

Done? Great!

Create  a file called composer.json add the following:

{
    "require": {
        "behat/behat":           "2.4@stable",
        "behat/mink-extension":  "*",
        "behat/mink-goutte-driver":     "*"
    },
    "minimum-stability": "dev",
    "config": {
        "bin-dir": "bin"
    }
}

Run composer install   and give it a minute to sort out and install dependencies. It’s like apt-get or gem but for php modules! Yey!

This will install of the dependencies into a vendor folder and link to the behat binaries from the bin folder.

Step 2: Setup Behat

Run bin/behat –init   to create the “features” folder with a few defaults.

Create a file called behat.yml in you project’s root folder (next to your composer.json file) with the following:

default:
  extensions:
    Behat\MinkExtension\Extension:
        base_url:  'http://en.wikipedia.org/'
        goutte: ~

Pay attention to the formatting. In Yaml, indentation dictates structure (as in Python it dictates control-flow) so its unusually easy to break something with zero compile-time syntax-based feedback.

Step 3: Setup the MinkExtension bootstrap

This is the only php you will have to edit. The MinkExtension context basically means you don’t have to write any php in order to perform typical web-related tests.

Replace the file features/bootstrap/FeatureContext.php with the following content:

<?php

use Behat\MinkExtension\Context\MinkContext;

class FeatureContext extends MinkContext
{
    public function __construct(array $parameters)
    {
    }
}

 Step 4: Create your first feature test.

Create a file called features/wiki-search-test.feature with the following content:

Feature: Search
  In order to see a word definition
  As a website user
  I need to be able to search for a word

  Scenario: Searching for a page that does exist
    Given I am on "/wiki/Main_Page"
    When I fill in "search" with "Behavior Driven Development"
    And I press "searchButton"
    Then I should see "software development process"

  Scenario: Searching for a page that does NOT exist
    Given I am on "/wiki/Main_Page"
    When I fill in "search" with "Glory Driven Development"
    And I press "searchButton"
    Then I should see "Search results"

Step 4: Profit

In your project’s root folder run: bin/behat

This will hit up the web server, access pages, send data, and grep the output. All behind the scenes.

If all tests pass, you should see:

Glorious Testing

To see all of the cool code-free one-liners that Mink provides, run: bin/behat -dl

Now go make more feature tests. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *