Jump to: navigation, search

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

Project state Work in progress
Tracker issue MDL-32323, MDL-32149, MDL-31857
Discussion [1]
Assignee Petr Škoda (škoďák)

Moodle 2.3

PHPUnit by Sebastian Bergmann is the most advanced unit testing framework for PHP.

Installation of PHPUnit support

PHPUnit is distributed as PEAR package, it is not part of Moodle installation. You have to manually install it on your dev computer. The installation procedure depends on your operating system or the way you installed PHP.

Installation steps:

  1. install or enable PEAR support in your PHP, see official PEAR installation guide
  2. upgrade PEAR, in *nix usually
    sudo pear upgrade PEAR
  3. install PHPUnit package as root or Windows administrator (minimum required version is 3.6.0):
pear config-set auto_discover 1
pear install

Initialisation of test environment

Our PHPUnit integration requires a dedicated database and dataroot. First add new dataroot directory and prefix into your config.php, you can find examples in config-dist.php

$CFG->phpunit_prefix = 'phpu_';
$CFG->phpunit_dataroot = '/home/example/phpu_moodledata';

Then you need to initialise the test environment using following command:

bash /home/example/moodle/admin/tool/phpunit/cli/



This command has to be used also after any upgrade, plugin (un)installation or adding tests to new plugin.

Test execution

To execute all test suites from main configuration file execute
script from your
cd /home/example/moodle

Use relative path to the test file for execution of one test only:

cd /home/example/moodle
phpunit lib/tests/phpunit_test.php

To execute all tests from one directory type:

cd /home/example/moodle
phpunit lib/tests/*_test.php
In IDEs you may need to specify path to configuration file, use absolute path to
from your

PHPUnit support in IDEs

See also