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](https://getcomposer.org/download/).
- [Drush](http://docs.drush.org/en/master/install/).
#### 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](/sites/default/files/blog/img/fzwxi9xhcr.png)
#### Preparing the site for testing
For testing create:
#####**Content Type**
![Creating Test CT](/sites/default/files/blog/img/screenshot_from_2016-05-24_09-07-45.png)
- Name: Test
- Description: Test content for behat
#### Configuring Behat to use Drupal
We check that the installation is successful running Behat
![Check Behat Version](/sites/default/files/blog/img/tearhhvjli.png)
vendor/behat/behat/bin/behat --version>
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
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](/sites/default/files/blog/img/screenshot_from_2016-05-24_10-58-12.png)
#### 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**
@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.
#### 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](/sites/default/files/blog/img/screenshot_from_2016-05-24_11-36-13.png)
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](http://www.rootstack.com/sites/default/files/drupal_behat.zip)