-
Notifications
You must be signed in to change notification settings - Fork 1
Writing your first test
Writing a Behat test has a cookbook approach. This will walk you with the assumption that your environment is already working, and you need to add a test to a branch you are working on.
Let's suppose you need to test that a student can reach their user profile from the control panel. This tests requires you to log into a course as student, find the control panel, and then find the link to edit the student's user profile. We'll make an edit, then verify it. So we can break it down to the following steps:
- log in as a student
- navigate to a course
- find the control panel
- find the link to edit the user profile
- make an edit
- verify
Since this tests some of the functionality of the Control Panel, we should add our feature file in there.
blocks/ucla_control_panel/tests/behat
A good name for this feature could be user_profile.feature
, so let's create the file:
blocks/ucla_control_panel/tests/behat/user_profile.feature
To start, tag your feature file with @ucla
and @block_ucla_control_panel
. We use the @ucla
for all our tests. The other tag tells us what plugin you are testing. Next you'll need to describe your feature. We use the 'user story' format for this.
@ucla @block_ucla_control_panel
Feature: Control Panel user profile
In order to edit my profile
As an student
I need to access the User Profile page through the control panel
This bit of code does not get executed. It's meant to be human readable. To start the executble
Remember that each test gets clean slate, so you need to set up the environment for your Feature. In this case, we need a course and a student enrolled in that course. This breaks down as:
- course
- student
- student enrolled in that course
Moodle gives us steps to do all this out of the box. With the following you can generate the user, the course, and the enrollment.
Given the following "users" exists:
| username | firstname | lastname | email |
| student1 | Student | 1 | student@asd.com |
And the following "courses" exists:
| fullname | shortname | format |
| Course 1 | C1 | ucla |
And the following "course enrolments" exists:
| user | course | role |
| student1 | C1 | student |
This the minimum amount of setup to get you started with the testing scenario.
To execute the test, you need to pretend you're the user. As a student, you want to log in, go to the course you're enrolled in, then visit the control panel and click on the 'Edit user profile' link. As an itemized list this can look like:
- Log in
- Follow course link
- Press control panel button
- Follow the 'Edit user profile' link
- Verify information
Given I log in as "student1"
And I follow "Course 1"
And I press "Control Panel"
When I follow "Edit user profile"
Then I should see "First name"
Behat requires you to describe your testing steps in a Scenario
. A single test can contain several scenarios, this way you can partition a test into smaller chunks. For this starting test, we're only doing a single scenario, so we'll describe it as:
Scenario: Editing user profile as student
Given the following "users" exists:
| username | firstname | lastname | email |
| student1 | Student | 1 | student@asd.com |
And the following "courses" exists:
| fullname | shortname | format |
| Course 1 | C1 | ucla |
And the following "course enrolments" exists:
| user | course | role |
| student1 | C1 | student |
# Environment is set up, begin testing steps
Given I log in as "student1"
And I follow "Course 1"
And I press "Control Panel"
When I follow "Edit user profile"
Then I should see "First name"
The scenario encapsulates a single test. If you were to add a separate scenario, you would need to generate the suitable environment state. For example, another test might require you to have an instructor and two more students. You would need to generate that data in the new scenario.
php admin/tool/behat/cli/util.php --enable
First, you checkout your branch. Since we're working on the assumption that you're adding a new plugin, you'll need to run the upgrade process via the console. But before you get to that, you'll need to update your git submodules.
So in order:
- Checkout your branch
- Update the git submodules
- Run the moodle upgrade via console
- Re-init the Moodle Behat environment