This documentation page is in progress and might suffer modifications.
Behat is an open source Behavior-Driven Development framework for PHP. Behat features are writen in Gherkin, which is a human readable language for writing (and at the same time documenting) test cases. A simple example to undestand the Gherkins syntax:
Feature: Refund item Scenario: Jeff returns a faulty computer Given: Jeff has bought a computer for $900 And: he has a receipt When: he returns the computer Then: Jeff should be refunded $900
Local Set Up
1. Install composer
To install the Behat dependencies will rely on Composer. Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
Check the Composer documentation on how to install and configure it in your development environment.
2. Install Behat and Mink extension
Under the root folder of your Moodle installation, run the following to install Behat:
composer require --dev behat/behat
Mink is an open source browser controller/emulator for web applications, written in PHP.
composer require --dev behat/mink composer require --dev behat/mink-extension composer require --dev behat/mink-goutte-driver composer require --dev behat/mink-selenium2-driver composer require --dev dvdoug/behat-code-coverage
3. Create dataroot folder for Behat
Create a new dataroot folder for Behat. It is recommended that you create this folder in the same directory as your moodledata folder. You can name this folder anyway you like, but a recommended name is moodledata_behat.
4. Update local configuration
Open the config.php file located in your moodle root location.
Search for the line
require_once(__DIR__ . '/lib/setup.php');
Paste the following code before the line indicated in the previous step:
$CFG->behat_dataroot = '/path/to/the/dataroot/you/created'; $CFG->behat_wwwroot = 'http://127.0.0.1/path/to/your/site'; $CFG->behat_prefix = 'beh_';
Replace the property
//Make sure you use localhost here instead of 127.0.0.1 $CFG->wwwroot = 'http://localhost/path/to/your/site';
You will need to set the
$CFG->behat_wwwrootto your Moodle site, but it must use a different value to your
One common way to do this is to use
127.0.0.1for behat, but
localhostfor standard use. Alternatively you can add an additional hostname in your
/etc/hostsfile and use this instead.
5. Initialise Behat
Next, we need to initialise the Behat testing environment, and create the tests site. For this, we first are going to
First, you need to clone the
moodle-browser-config repository in the root folder of your Moodle instalation:
After the repository is succesfully downloaded, we need to add it as a requirement in the
before the line
require_once( __DIR__ . '/lib/setup.php') :
require_once( __DIR__ . '/moodle-browser-config/init.php');
Then, we can run the Behat script to create the test environment and site:
Be patient, this process can take a while. After it completes succesfully, a message like the following will appear:
Acceptance tests environment enabled on http://127.0.0.1/moodle, to run the tests use: vendor/bin/behat --config /var/moodledata_behat/behatrun/behat/behat.yml
6. Add MOODLE_ROOT environment variable
7. Run the oppia_mobile_export block tests
### Using Selenium
Start a Selenium standalone server
cd oppia_mobile_export/tests/lib java -jar selenium-server-4.5.3.jar standalone &
Run the features
# From the root folder of your moodle installation ./vendor/bin/behat --config blocks/oppia_mobile_export/tests/behat/behat.yml
### Using Geckodriver (Firefox)
Start the geckodriver server
Run the features
# From the root folder of your moodle installation ./vendor/bin/behat --config blocks/oppia_mobile_export/tests/behat/behat.yml --profile=geckodriver
Writing new Behat features
You should locate all new features in oppia_mobile_export/test/behat. Behat will run all .feature files located in that folder. For extending an existing feature, create a new Scenario clause in the feature file.
HTML Report and Code Coverage
When the Behat tests have finished, two new folders should have been created:
tests/behat/report: This folder contains the HTML report of the Behat run. Open the index.html file in your browser to see it.
tests/behat/CodeCoverage: This folder contains the code coverage report in HTML format.