PHPUnit

Jump to: navigation, search

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.

PHPUnit
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 pear.phpunit.de/PHPUnit
pear install phpunit/DbUnit

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/init.sh

or

c:\server\moodle\admin\tool\phpunit\cli\init.bat

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
phpunit
script from your
$CFG->dirroot
directory:
cd /home/example/moodle
phpunit

Use relative path to the test file for execution of one test only (test case classname followed by path to test file):

cd /home/example/moodle
phpunit core_phpunit_basic_testcase lib/tests/phpunit_test.php
In IDEs you may need to specify path to configuration file, use absolute path to
phpunit.xml
from your
$CFG->dirroot
.

PHPUnit support in IDEs

See also