Setup up Behat on a new PHP project

Submitted by piofernandes on Thu, 01/31/2019 - 10:31

In order to take advantage of Behaviour Driven Development, I am installing Behat. This always for test coverage when integrating new features on the project, detecting regression errors automatically, before any deployment phase.

 

First of all, I create [project_root]/tests folder and inside, I add a "composer.json" file with the following content:


{ 
  require": {
    "behat/behat" : "*",
    "behat/mink": "*",
    "behat/mink-extension": "*",
    "behat/mink-goutte-driver": "*"
  }
} 

After this, I run (in the tests directory), the composer install command.

I create a behat.yml file, in the same directory, with the following:


default:
  extensions:
    Behat\MinkExtension:
      base_url: http://behat_test.local
      sessions:
        default:
          goutte: ~	

 

Next, the initialisation, with: vendor/bin/behat --init

To now use the Mink pre-built tests steps, I update my [project_root]/tests/features/bootstrap/FeatureContext.php with the following:



use Behat\MinkExtension\Context\MinkContext;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends MinkContext
{
    public function __construct()
    {
    }
}

And I create a simple test file named [project_root]/tests/features/example.feature with the following:


Feature: My example feature
  Scenario: My example scenario
    Given I am on "/"
    Then I should see "Dummy index test"

The next step, and final, is to run the test feature with: vendor/bin/behat features/example.feature and the result is the following:

behat test result