Behat Testing

Behat is a test framework for behavior-driven development written in the PHP. We use behat to automate the testing of a particular feature. There’s a some effort needed to install and run behat test. This post will cover everything from installation to write feature script.

Installing Behat:

First go to your moodle repository

  cd /var/www/html/moodle

For configuring behat need some edit in config.php for specifying the location of the directory where the data is going to be stored and also a different port address, add following lines in config.php file.

  $CFG->behat_dataroot = '/var/www/behat_moodle';
  $CFG->behat_prefix = 'behat_';
  $CFG->behat_wwwroot='http://localhost:4000';

Now open terminal and install composer

  curl http://getcomposer.org/installer | php

if you haven’t installed curl then beofre executing the above command you have to install curl

  sudo apt-get install curl

You need to host Moodle to another address.

  php -S localhost:4000

Now initialize behat which will also refresh behat.yml which has the list of all of the feature files in the directory.

  php admin/tool/behat/cli/init.php

For keeping track on background script which are running in your browser at the time when the feature file will be executed, you have to download selenium(selenium-2.53.1 has a great compatibility with firefox- 47.0.1 ) Open another tab in terminal and execute the following command

  java -jar ~/selenium/selenium-server-standalone-2.53.1.jar

For updating available tests(in case if you have added a .feature file in any of the directory).

  php admin/tool/behat/cli/util.php --enable

Final step for configuring all feature file.

  vendor/bin/behat --config /path/to/your/behat/dataroot/directory/behatrun/behat/behat.yml

Selenium will open a browser (firefox by default) and you will see how the steps you have been writting are executed. In a single run it will take a 18-24 hrs to complete all the steps. So it is preferred to run 3 parallel run. So there will be three browser’s and three selemnium hosted at three different ports. For doing so you have to add few more lines to config.php and run init.php.

  $CFG->behat_parallel_run = array (
    array ('wd_host' => 'http://127.0.0.1:4444/wd/hub'),
    array ('wd_host' => 'http://127.0.0.1:4445/wd/hub'),
    array ('wd_host' => 'http://127.0.0.1:4446/wd/hub')
  );

For configuring in another(chrome) browser. Before that make sure that you had installed chromedriver .

  $CFG->behat_config = array('default' => array('browser' => 'chrome'));

Afer completing the all the test, there’s no need of running all tests for a single fearture file which is added. You just have to refrest behat.yml and show show the path where it is stored.

  vendor/bin/behat --config /var/www/behat_moodle/behatrun/behat/behat.yml /path/to/your/file.feature 

For more details on writing tests and configuring behat please got through the Moodle docs on acceptance test.

 

Advertisements