Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Behat: Difference between revisions

From MoodleDocs
No edit summary
No edit summary
Line 1: Line 1:
{{Work in progress}}
{{Work in progress}}


== Introduction ==
This page describes how we describe Moodle functionalities and how we automatically test all of this Moodle's functionalities.


Behat is a Behaviour Driven Development (BDD) framework, allows development and testing guided by steps to simulate users interaction.
Behat is a behavioural driven development (BDD) tool written in PHP, it can parse a human-readable list of sentences (called steps) and execute actions in a browser using Selenium or other tools to simulate users interactions.
* Support for multiple browsers drivers (including selenium 2) http://mink.behat.org/#different-browsers-drivers
* Extension to connect to Jira (linking issues to features)


This document is a work in progress (STABLE team week off project) and at the moment it should not be taken into account for nothing out of this scope nor nothing official.
(This is not in standard Moodle yet: to play a bit with it now clone https://github.com/moodlehq/moodle-behat-features and follow the installation and usage instructions.)


=== How it works ===
Behat parses and executes features files which describes Moodle features (for example ''Post in a forum''), each feature file is composed by many scenarios (for example ''Add a post to a discussion'' or ''Create a new discussion''), and finally each scenario is composed by steps (for example  ''I press "Post to Forum"'' or ''I should see "My post title"''). When the feature file is executed, every step internally is translated into an PHP method and is executed.


== Example ==
This features are executed nightly in the HQ servers with all the supported databases (MySQL, PostgreSQL, MSSQL and Oracle) and with different browsers (Firefox, Internet Explorer, Safari and Chrome) to avoid regressions and test new functionalities.
The expected behaviours are specified as scenarios.


=== Basic scenarios ===
=== Examples ===


   Scenario: Login as an existing user
* There is a closed list of steps to use in the features, a feature written with the basic (or low-level) steps looks like this:
     Given I am on "login/index.php"
   Feature: Login
    When I fill in "username" with "admin"
    In order to login
    And I fill in "password" with "moodle"
    As a moodle user
    And I press "loginbtn"
    I need to be able to validate the username and password against moodle
    Then I should see "Moodle 101: Course Name"
    Scenario: Login as an existing user
      Given I am on "login/index.php"
      When I fill in "username" with "admin"
      And I fill in "password" with "moodle"
      And I press "loginbtn"
      Then I should see "Moodle 101: Course Name"
     Scenario: Login as an unexisting user
      Given I am on "login/index.php"
      When I fill in "username" with "admin"
      And I fill in "password" with "moodle"
      And I press "loginbtn"
      Then I should see "Moodle 101: Course Name"


  Scenario: Login as an unexisting user
Note that The 3 sentences below ''Feature: Login'' are only information about what we want to test.
    Given I am on "login/index.php"
    When I fill in "username" with "adminasdasd"
    And I fill in "password" with "moodlesdfasdf"
    And I press "loginbtn"
    Then I should see "Invalid login, please try again"


=== Complex scenario ===
These are simple scenarios, but most of Moodle functionalities would require a huge list of this steps to test a scenario, imagine a ''Add a post to a discussion'' scenario; you need to login, create a course, create a user and enrol it in the course... Most of this steps is not what we intend to test in a ''Post in a forum'' feature, Moodle provides extra steps to quickly set up the context required to test a Moodle feature, for example:
The aim of this project is to be able to define scenarios like this. Note the quoted strings are received as variables by the steps definitions which will process them.


  Scenario: A teacher adds a discussion
  Feature: Post in a forum
    Given I am logged as a "teacher"
    In order to add contents to a forum
    And I go to a course
    As a moodle user
    And I create a "forum" activity
    I need to be able to enter the contents and verify they are correctly shown
    And I view the "forum" activity    # The step definition class will keep a reference to the lasts steps
    Background:
    When I add a "forum_discussion" filling the fields "subject,message[text]" with "I'm the user subject,Dealing with \, split"   #     Wrapper step, it will redirect the petition to a "forum_discussion" method
      Given the following "courses" exists
    Then I should see "Discussion"
        | fullname | shortname |
    And I should see "Subject"
        | Course 1 | C1        |
    And I should see "I'm the user subject"
      And the following "users" exists
        | username | firstname | lastname | email        |
        | student1 | First     | Student  | stu1@asd.com |
      And the following "enrolments" exists
        | username | courseshortname | enrolmenttype |
        | student1 | C1              | manual        |
    Scenario: Create a forum
      Given I loggin as an "admin"
      And I go to "Course 1" course
      And I add a "forum" to section "1"
      And I fill the form with
        | Forum name        | ForumTest1                     |
        | Forum description | Test description               |
        | Forum type        | Standard forum for general use |
        | Any other field   | Value                          |
      When I press "Save and display"
      Then I should see "Test1"
      And I should see "Add a new discussion topic"
     Scenario: Add a post to a discussion
      Given I run "Create a forum" scenario
      And I go to "Course 1" course
      And I follow "ForumTest1"
      When I press "Add a new discussion topic"
      And I fill the form with
        | Subject | test subject |
        | Message | message body |
      And I press "Post to forum"
      Then I should see "test subject"


In order to be able to process the "logged as a $roleshortname" or the "a course" statements the /behat.yml file should be filled with your own installation data
Note that:
 
* Background is executed before each scenario execution
=== Input Tables ===
* Each scenario is executed in an isolated testing environment, so what you set up in an scenario (like the ''ForumTest1'' forum in the example above) is cleaned up after the scenario execution
To improve readability and reduce unnecessary verbosity; complex scenarios can use input tables to group similar types of input:
* The prefixes "Given", "When" and "Then" are only informative and they as used to define the context (Given), specify the action (When) and check the results (Then).
 
  Scenario: Add Offline text assignment
    Given that I add "assignment" to section "1"
    And I fill in the form:
      | field_name      | value  |
      | Assignment name  | Test3  |
      | Description      | Test3  |
      | Online text      | No    |
      | File submissions | No    |
    And I click "Save and display"
    Then the title "Test3" should be displayed
 
or even:
 
  Scenario: Set Submissions from date
    Given that I add "assignment" to section "1"
    And I fill in the form:
      | field_name            | value |
      | Assignment name        | Test4 |
      | Description            | Test4 |
    And I enter the dates in "Allow submissions from":
      | date_unit | value    |
      | day      | 1        |
      | month    | September |
      | year      | 2012      |
      | hour      | 09        |
      | minute    | 05        |
    And I click "Save and display"
    Then the title "Test4" should be displayed
 
=== Background Steps ===
 
In a feature where certain steps are repeated in every scenario, a feature background can be defined. The feature background will be repeated just before every scenario in the feature:
 
  Background: 
    Given I am logged in as a "teacher"
    And I go to a course
 
This also helps to increase readability and reduce the verbosity of the tests.
 
=== Teardown ===
 
Most automated tests require teardown steps after each test has run. The teardown ends the test session, closes browsers etc. Behat does this automatically after every scenario, so you don't have to worry about adding any teardown steps.
 
== Packages ==
There are two packages, one with the features files (scenarios like the above examples) and a package with Moodle contexts and steps definitions (PHP code to interact with selenium2). Only the features project is required since it manages all it's dependencies, including the Moodle contexts
 
== Requirements ==
* PHP 5.3.3
* PHPUnit
* There are other packages and PHP settings requirements, but they be checked by the composer installer
 
== Installation ==
* Clone the Moodle features project: https://github.com/moodlehq/moodle-behat-features
* Follow the [https://github.com/moodlehq/moodle-behat-features/blob/master/README.md README] instructions
 
== Usage ==
* To test a feature type "bin/behat features/FEATURENAME.feature" from the clone directory
* If you want to execute the feature in a Selenium Grid2 you can specify another behat profile: "bin/behat --profile seleniumGrid2Firefox features/FEATURENAME.feature"
 
== Update to latest versions ==
Behat uses the composer dependencies management system, all the project dependencies can be updated with two commands.
* cd to behat directory
* ./composer.phar self-update
* php composer.phar update
 
== Development environment ==
One of 'moodle-behat-features' dependencies is 'moodle-behat-contexts', which is a work in progress, to set up the development environment to work/commit to both projects with the composer dependency management system advantages, after installing the moodle-behat-features according to it's README file, you can git clone the moodle-behat-contexts project into vendor/moodlehq/behat-contexts and name your remote as 'composer' when you update your packages composer reads this remote name and updates the 'moodle-behat-contexts' package from the remote repository you specified.
 
== Steps ==
Every step is processed in a step definition, which is defined in "context" classes. The Behat project has a main context class, FeaturesContext, but it allows more contexts to be added as subcontexts, Moodle is one of this subcontexts, all the Moodle steps definitions are split in a modular way following the [[https://docs.moodle.org/dev/Frankenstyle Frankenstyle]] format, and are loaded dynamically by Moodle\Behat\Context\MoodleContext.
 
A quick view of the hierarchy:
 
* FeaturesContext
** Sanpi/Behatch/Context
*** Behat extension with a lot of common actions like send REST petitions, store screenshots, DOM / XPath parsing
** Moodle/Behat/Context
*** MoodleContext as a reference to the rest of the Moodle components
*** BaseContext abstract class with helper methods
**** CoreContext
***** contains basic steps definitions like visit($url), pressButton($button) to manage the browser
***** The cross-component and the most common Moodle actions like iAmLoggedAsA($roleshortname) or aUserAssignedInContextOfContextlevel($roleshortname, $instanceid, $contextlevel) to speed up the creation of scenarios
**** CourseContext
***** Course related steps definitions
**** ModForumContext
***** mod_forum related steps definitions
**** ...
 
In a future with a lot of Moodle components, having a single shared directory could not be the best way to store the contexts, other directory hierarchies more similar to a moodle distribution can be made without changing references between components since they are currently made using the Frankenstyle component name.


== Links ==  
== Links ==  
[http://www.github.com/dmonllao/moodle-behat-features Moodle features project]
Technical info: https://docs.moodle.org/dev/Behat_integration
 
[http://www.github.com/dmonllao/moodle-behat-contexts Moodle contexts repository]

Revision as of 06:16, 16 October 2012

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.


Introduction

This page describes how we describe Moodle functionalities and how we automatically test all of this Moodle's functionalities.

Behat is a behavioural driven development (BDD) tool written in PHP, it can parse a human-readable list of sentences (called steps) and execute actions in a browser using Selenium or other tools to simulate users interactions.

(This is not in standard Moodle yet: to play a bit with it now clone https://github.com/moodlehq/moodle-behat-features and follow the installation and usage instructions.)

How it works

Behat parses and executes features files which describes Moodle features (for example Post in a forum), each feature file is composed by many scenarios (for example Add a post to a discussion or Create a new discussion), and finally each scenario is composed by steps (for example  I press "Post to Forum" or I should see "My post title"). When the feature file is executed, every step internally is translated into an PHP method and is executed.

This features are executed nightly in the HQ servers with all the supported databases (MySQL, PostgreSQL, MSSQL and Oracle) and with different browsers (Firefox, Internet Explorer, Safari and Chrome) to avoid regressions and test new functionalities.

Examples

  • There is a closed list of steps to use in the features, a feature written with the basic (or low-level) steps looks like this:
 Feature: Login
   In order to login
   As a moodle user
   I need to be able to validate the username and password against moodle
   Scenario: Login as an existing user
     Given I am on "login/index.php"
     When I fill in "username" with "admin"
     And I fill in "password" with "moodle"
     And I press "loginbtn"
     Then I should see "Moodle 101: Course Name"
   Scenario: Login as an unexisting user
     Given I am on "login/index.php"
     When I fill in "username" with "admin"
     And I fill in "password" with "moodle"
     And I press "loginbtn"
     Then I should see "Moodle 101: Course Name"

Note that The 3 sentences below Feature: Login are only information about what we want to test.

These are simple scenarios, but most of Moodle functionalities would require a huge list of this steps to test a scenario, imagine a Add a post to a discussion scenario; you need to login, create a course, create a user and enrol it in the course... Most of this steps is not what we intend to test in a Post in a forum feature, Moodle provides extra steps to quickly set up the context required to test a Moodle feature, for example:

  Feature: Post in a forum

   In order to add contents to a forum
   As a moodle user
   I need to be able to enter the contents and verify they are correctly shown
   Background:
     Given the following "courses" exists
       | fullname | shortname |
       | Course 1 | C1        |
     And the following "users" exists
       | username | firstname | lastname | email        |
       | student1 | First     | Student  | stu1@asd.com |
     And the following "enrolments" exists
       | username | courseshortname | enrolmenttype |
       | student1 | C1              | manual        |
   Scenario: Create a forum
     Given I loggin as an "admin"
     And I go to "Course 1" course
     And I add a "forum" to section "1"
     And I fill the form with
       | Forum name        | ForumTest1                     |
       | Forum description | Test description               |
       | Forum type        | Standard forum for general use |
       | Any other field   | Value                          |
     When I press "Save and display"
     Then I should see "Test1"
     And I should see "Add a new discussion topic"
   Scenario: Add a post to a discussion
     Given I run "Create a forum" scenario
     And I go to "Course 1" course
     And I follow "ForumTest1"
     When I press "Add a new discussion topic"
     And I fill the form with
       | Subject | test subject |
       | Message | message body |
     And I press "Post to forum"
     Then I should see "test subject"

Note that:

  • Background is executed before each scenario execution
  • Each scenario is executed in an isolated testing environment, so what you set up in an scenario (like the ForumTest1 forum in the example above) is cleaned up after the scenario execution
  • The prefixes "Given", "When" and "Then" are only informative and they as used to define the context (Given), specify the action (When) and check the results (Then).

Links

Technical info: https://docs.moodle.org/dev/Behat_integration