Behat in stages
If you are after examples of a certain Behat functionality (e.g. tabular data in test code), this repo could be useful.
The code in this repo tries to demonstrate as many ways you can write Behat test code as possible. In my article, I have introduced various Behat concepts in seven stages. The test suites in this repo corresponds with those stages. Following topics are covered:
|Stage 1||How to connect Gherkin steps with PHP step definitions.|
|Stage 6||Multiple features in a single test suite.|
The code in stage 6 suffers from code repetition. I took this opportunity to demonstrate the use of Behat profiles. I have created three profiles which override stage 6 from the default profile in three different ways all of which avoid code repetition:
|Trait stage||We use PHP traits to avoid code repetition.|
|Service stage||We create a service class that contains the common functionality and rely on Behat's built-in service container to pass it to our PHP class constructors.|
|Service autowire stage||Same as the service stage, but we rely on service autowiring.|
If you are after Behat examples of a certain type, the previous two tables should guide you to the right test suite.
$ git clone https://github.com/progga/behat-in-stages.git $ cd behat-in-stages/ $ cp .env.dist .env # Optional step. See Configuration notes below. $ composer install
Test target (a fresh Drupal site)
$ composer create-project --stability dev --no-interaction --no-dev drupal-composer/drupal-project behat-test-target $ cd behat-test-target/ $ ./vendor/bin/drush --yes site-install --db-url=sqlite:///path/to/behat-test.sqlite --account-pass=RANDOM-PASSWD $ cp /path/to/behat-test.sqlite /path/to/behat-in-stages/tests/test-db/behat-test.sqlite $ ./vendor/bin/drush runserver
- The admin password for the Drupal site must go into the behat-in-stages/.env file. Alternatively, you can drop the .env file and provide it through the BEHAT_DRUPAL_ADMIN_PASSWD environment variable.
- The provided behat.yml file uses /dev/shm/ to store the Drupal site's password. This only works in GNU/Linux where /dev/shm is a memory backed file system with quicker access. On other Operating systems, use a different path and update behat.yml accordingly.
$ cd behat-in-stages/ $ composer test
$ cd behat-in-stages/ $ ./vendor/bin/behat --format=progress $ ./vendor/bin/behat --format=progress --profile=dry-with-traits $ ./vendor/bin/behat --format=progress --profile=dry-with-services $ ./vendor/bin/behat --format=progress --profile=dry-with-autowired-services $ ./vendor/bin/behat --format=progress --suite=stage-two --stop-on-failure # Test only stage-two
Software versions used
- Behat: 3.4
- PHP: 5.6 or 7.x
- PHP extensions: curl, gd, pdo-sqlite, sqlite3
- composer: 1.6+
- SQLite: 3.x