For this year's Drupal Camp a joint effort has been made to offer three different workshops on testing with Drupal. One of them was a Behat workshop where a theoretical basis was given and different practical exercises with Behat were proposed.
Through this article we want to make this same Behat workshop available to anyone who is interested so that they access the presentation and the exercises. Unfortunately, the slides are only available in Spanish. However, the exercises are written in English so even if you don't speak Spanish you may benefit of doing the practical part.
The workshop
The workshop uses two repositories, the first is a presentation used as a guide for the workshop, and the second is a pre-configured environment with Behat ready to be run.
The presentation can be viewed online directly from the browser. It consists of three parts:
- A very basic introduction to Behat, supported by more information during the practical part.
- How to configure the environment to perform the exercises using the second repository. This step is very simple as it is based on Metadrop's Boilerplate, which includes a set of Docker containers already configured to run Drupal and Behat (as well as other tools).
- The practical part, where exercises are presented in the form of user stories to be written using Behat.
The interesting thing about the practical part is that all the exercises have a solution that can be consulted to validate the exercise. This allows the doer to have at least one reference solution to each problem.
How to
First go to the workshop's slides. Read the first part with the introduction to Behat. Since it is a presentation, there are not many details but you can learn more in the official Behat documentation or in other references you can find.
For the practical part, check the slide "Montar entorno" (which is duplicated, once at the beginning of the presentation and then at the beginning of the practical part), where you can find the the three simple commands required:
- Clone the repo:
git clone git@github.com:rsanzante/drupalcampspain2023-behat-workshop.git
- Install PHP pacakges (run inside the cloned repository):
composer install
- Run the assistant to configure the environment:
composer boilerplate:assistant
The assitant will ask several questions but the default values are fine except the profile to install: you must select the Umami profile. This is because the workshop makes use of the functionalities and content of this demo profile that comes with the Drupal core. It also asks whether to create a Radix subtheme; although we can leave the default option (yes), it is not required for the workshop and it takes a while to generate it, so it's better not to generate it.
The exercise repository contains a workshop branch and several tags:
Commits and repository tags from the practical part. The dev branch and "Initial commit" commit are generated by the assistant and can be ignored (or select not to initialise the Git repository so that it doesn't add the branch and commit).
Start by jumping to the first tag, initial-setup, and test that Behat works (make shell
and then just behat
). Then, checkout the tag of the first exercise (us1-exercise) and check the directory tests/functional/behat/features/umami/ to find the exercise. Once done, you can checkout the next tag to see the suggested solution (us1-solution). This cycle is repeated for all exercises.
Each exercise is completed in a file and the solution is in another file, so that both solutions can coexist (the suggested one and the one wrote by the person doing the workshop). In fact, the suggested solutions are not executed because the files do not have the .feature
extension. The last commit renames the suggested solutions by adding this extension, so that these solutions can be tested quickly and easily.