Performing automated tests in Drupal with Behat

June 18, 2021


Table of contents

Quick Access

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. #### Requirements - Installation of Drupal 7. - You can also create a test installation using drush qd test_behat --core=drupal-7.x -y - [Composer]( - [Drush]( #### Installing dependencies 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. ![Composer vendor files]( #### Preparing the site for testing For testing create: #####**Content Type** ![Creating Test CT]( - Name: Test - Description: Test content for behat #### Configuring Behat to use Drupal We check that the installation is successful running Behat ![Check Behat Version](
vendor/behat/behat/bin/behat --version>
Create the file **behat.yml** where we indicated Behat configuration for this specific project.

'': %paths.base%/features/bootstrap
    - %paths.base%/features/tests
    - FeatureContext
    - Drupal\DrupalExtension\Context\DrupalContext
    - Drupal\DrupalExtension\Context\DrushContext
    - Drupal\DrupalExtension\Context\MinkContext
  goutte: ~
  base_url: http://localhost:8888/  # Replace with your site's URL
  blackbox: ~
  api_driver: drupal
    root: %paths.base%
    drupal_root: %paths.base%
> Based on behat.yml.dist Execute `vendor/behat/behat/bin/behat --init` which will create the folder **features/** and create a base file where you can add custom steps. ![Run Behat init]( #### Creating a test Behat We will create the basic files for performing a functional test of Behat. - At the root of the project. - Create the folder `./features/tests/` - We will create a file called ./features/tests/content_types.**feature**

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. #### Running test with Behat Now we proceed to our test run using the following command:
vendor/behat/behat/bin/behat features/tests/content_types.feature
![Behat Test Result]( 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: [](http://www.rootstack.com