-
Notifications
You must be signed in to change notification settings - Fork 1
Environment basics
Behat tests live inside the tests
directory inside moodle plugins. This is where you will also find unit tests and other relevant testing assets. Moodle looks for Behat tests in the test\behat
folder. So you will usually have:
blocks\ucla_course_download\tests\behat
local\ucla\tests\behat
And so forth.
Behat tests are written in files with the feature
extension. This is where you write the steps that your test will execute.
Sometimes you will need to write your unique steps for your plugin, this is where you place those files. The structure of the PHP code is a class that usually extends behat_base
. This not classical inheritance, you cannot override the base class methods. Instead Behat provides you with contexts. In this way, you can create subcontexts of other Behat classes and use those classes' methods.
Moodle uses a custom Behat plugin that does a lot of the hard work. It's part of a plugin that is loaded with Composer. This plugin takes care of loading all the required testing steps and Behat helper libraries. All you really need to provide is the behat.yml
file that contains configuration data and tells Behat where to find tests and custom steps.
As mentioned prior, Moodle looks for Behat tests in the defined folder structure. When you are in a testing environment context you will need to run a few scripts to tell Moodle when it needs to revisit that folder structure to find new test files. These scripts basically rebuild your behat.yml
file.
The primary script that sets up Behat in Moodle is found in admin\tool\behat\cli\init.php
. When you run this, a few things can happen.
-
If it is the first time you use your environment (or you've introduced new code), Moodle will launch a setup script that basically clones your database tables with a different prefix. Don't worry about the specifics of this, just know that it may take a while.
-
Moodle will upgrade its behat plugin via Composer.
-
Moodle will generate the
behat.yml
file that tells the Moodle Behat plugin where to find all the tests.
A common problem that occurs here is that you will forget to update your git submodules when you run the init.php
script. ALWAYS run git submodule init && git submodule update
to update your submodules. Failure to do so will likely result in your database becoming corrupt. You have been warned!
This is another script found in admin\tool\behat\cli\util.php
that is used as a switch with two flags: --enable
and --disable
. Since we usually reserve a separate Moodle instance specifically for testing, we don't necessarily need to disable our environment. When you toggle the switch, however, it will result in the rebuild of the behat.yml
file. When you run the --enable
flag, you implicitly run the init.php
script.
After you run the init
or enable
scripts, Behat will print out the command you need to run to run all Behat tests. It usually looks like so:
vendor/bin/behat --config /Users/alfonso/Sites/moodledata/behat/behat/behat.yml
You basically paste that into your console and run it. It will run all Behat tests in Moodle. This may not be what you want, you usually want to run the test you just created. To do so, add the --tags
flag. We use tags to identify tests.
vendor/bin/behat --config /Users/alfonso/Sites/moodledata/behat/behat/behat.yml --tags @block_ucla_course_download
If you want to get more descriptive feedback from the console about what step is running, you can set the --format
flag.
vendor/bin/behat --config /Users/alfonso/Sites/moodledata/behat/behat/behat.yml --tags @my_feature_test --format pretty
This will print the running steps and associated PHP functions in the console.