Behat is an open source behavior-driven development tool for PHP. You can use Behat to build and run automated tests for site functionality on your Drupal sites, and Drupal VM has excellent built-in support for Behat, using Selenium to run tests in a headless instance of FireFox.
Getting Started - Installing Prerequisites¶
To make Behat available globally for all your projects within Drupal VM, make the following changes inside config.yml
, then run vagrant up
(or vagrant provision
if the VM is already built):
# Make sure selenium is not commented out in the list of installed_extras:
installed_extras:
[...]
- selenium
[...]
# Make sure the following four packages are in composer_global_packages:
composer_global_packages:
- { name: drupal/drupal-extension, release: '*' }
After Drupal VM is finished provisioning, you should be able to log in and run the following command to make sure Behat is installed correctly:
$ behat --version
behat version 3.0.15
You can also include drupal/drupal-extension
directly in your project's composer.json
file, and install the dependencies per-project. Either option (installing globally, like above, or installing per-project) is perfectly acceptable.
Setting up Behat for your project¶
Using the default Drupal site as an example (it's installed in /var/www/drupalvm/drupal
by default, and is shared to ~/Sites/drupalvm/drupal
on your host machine), the following steps will help you get your first Behat tests up and running!
-
Create a
behat.yml
file inside the docroot of your site (e.g. create this file alongside the rest of the Drupal codebase at/var/www/drupalvm/drupal/behat.yml
), with the following contents:default: suites: web_features: paths: [ %paths.base%/features/web ] contexts: - WebContext - Drupal\DrupalExtension\Context\DrupalContext - Drupal\DrupalExtension\Context\MinkContext - Drupal\DrupalExtension\Context\MessageContext - Drupal\DrupalExtension\Context\DrushContext extensions: Behat\MinkExtension: goutte: ~ javascript_session: selenium2 selenium2: wd_host: http://drupalvm.dev:4444/wd/hub base_url: http://drupalvm.dev Drupal\DrupalExtension: blackbox: ~ api_driver: 'drupal' drupal: drupal_root: '/var/www/drupalvm/drupal' region_map: content: "#content"
-
Log into Drupal VM with
vagrant ssh
, change directory to the Drupal site root (cd /var/www/drupalvm/drupal
), then runbehat --init
to initialize thefeatures
folder where you will place test cases. -
From either inside the VM or on the host machine, open up the new
features/web
folder Behat just created. Inside that folder, createHomeContent.feature
with the following contents:Feature: Test DrupalContext In order to prove the Behat is working correctly in Drupal VM As a developer I need to run a simple interface test Scenario: Viewing content in a region Given I am on the homepage Then I should see "No front page content has been created yet" in the "content"
-
Now, inside Drupal VM, change directory to
/var/www/drupalvm/drupal
again, and run the commandbehat
(which runs all the tests you've created—which should just be one so far).
If everything was done correctly, you should see:
$ behat
Feature: Test DrupalContext
In order to prove the Behat is working correctly in Drupal VM
As a developer
I need to run a simple interface test
Scenario: Viewing content in a region # features/drupal/HomeContent.feature:6
Given I am on the homepage # Drupal\DrupalExtension\Context\MinkContext::iAmOnHomepage()
Then I should see "No front page content has been created yet" in the "content" # Drupal\DrupalExtension\Context\MinkContext::assertRegionText()
1 scenario (1 passed)
2 steps (2 passed)
0m0.56s (26.48Mb)
Hooray! Now you're ready to get started testing ALL THE THINGS! Check out the following resources for more information about Behat and Drupal: