Behat is a framework for testing based on behaviors, this can be very useful to ensure that during the development of a project any changes affecting previously implemented features will be prevented, thus ensuring that the overall functionality of the project is not affected at any stage of development due to bugs caused by some new feature.
To facilitate the process, all commands will be executed at the root of the project.
To use Behat must install a set of libraries which we will obtain by using the following commands composer
composer require behat/behat composer require drupal/drupal-extension composer require drupal/drupal-driver
We can see that the units are now available in the folder / vendor project.
For testing create:
We check that the installation is successful running Behat
Create the file behat.yml where we indicated Behat configuration for this specific project.
default: autoload: '': %paths.base%/features/bootstrap suites: default: paths: - %paths.base%/features/tests contexts: - FeatureContext - Drupal\DrupalExtension\Context\DrupalContext - Drupal\DrupalExtension\Context\DrushContext - Drupal\DrupalExtension\Context\MinkContext extensions: Behat\MinkExtension: goutte: ~ base_url: http://localhost:8888/ # Replace with your site's URL Drupal\DrupalExtension: blackbox: ~ api_driver: drupal drush: root: %paths.base% drupal: drupal_root: %paths.base%
Based on behat.yml.dist
vendor/behat/behat/bin/behat --init which will create the folder features/ and create a base file where you can add custom steps.
We will create the basic files for performing a functional test of Behat.
@api Feature: Tests can be done with the test content type Scenario: An administrator can see the content type at admin page. Given I am logged in as a user with the "administrator" role When I visit "/admin/structure/types" Then I should see "Test" Scenario: An administrator can see the tests nodes at content. Given I am logged in as a user with the "administrator" role And "Test" content: | title | body | | Test Node A | Some content | | Test Node B | Another content | | Test Node C | Content String | When I visit "/admin/content" Then I should see "Test Node A" And I should see "Test Node B" And I should see "Test Node C" Scenario: An user can see a created node. Given I am an anonymous user And "Test" content: | title | body | path[alias] | | Test Node D | Some content for testing | testnoded | When I am on the homepage And I follow "Test Node D" Then I should see the heading "Test Node D" And I should see "Some content for testing"
The Behat tests use the Gherkin language, where conditions are defined to confirm the correct operation of a software through steps that do not require programming knowledge to understanding.
Each file defines a specific feature and can have different settings for it.
Now we proceed to our test run using the following command:
And finally we will have our Drupal installation enabled for automated test using Behat, something that is very useful to ensure the integrity and avoiding errors caused in the development and regressions.
To test the environment used for this Blog download the following file: drupal_behat.zip