DEVELOPMENT by Pedro Resende

How to use Behat with Symfony

BDD: Behavior-Driven Development with symfony

Post by Pedro Resende on the 14 of August of 2015 at 17:37

Today, I've decided to start writting bdd tests into my Symfony projects. However the available documentation isn't quite the most straight forward. The steps are quite simples, to beging with, start by installing a new Symfony project

$ symfony new bdd

Enter your symfony project and run the following composer

$ composer require behat/behat behat/symfony2-extension behat/mink-extension behat/mink behat/mink-goutte-driver behat/mink-selenium2-driver

Let's edit a new behat.yml file and add the following content

default:
    extensions:
        Behat\Symfony2Extension: ~
        Behat\MinkExtension:
            base_url: http://en.wikipedia.org
            goutte: ~
            selenium2: ~
    suites:
        my_suite:
            type: symfony_bundle
            bundle: AppBundle

Let's now initiate our behat project, by running

$ bin/behat --init --suite=my_suite

To finished edit your FeatureContext file, located in features/bootstrap/AppBundle/Features/Context/FeatureContext.php and replace it's content by

<?php

namespace AppBundle\Features\Context;

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

use Behat\MinkExtension\Context\MinkContext;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends MinkContext
{

}

Finally to verify that everything is fine, run

$ bin/behat -dl

You should see the list of mink definitions.

Now you can start implementing your feature in a new feature file in side your bundle :)