|Project state||Work in progress|
|Tracker issue||MDL-32323, MDL-32149, MDL-31857|
|Assignee||Petr Škoda (škoďák)|
PHPUnit by Sebastian Bergmann is the most advanced unit testing framework for PHP.
You need to install PHP on your own computer. If you already have it installed you can skip this stage.
Real operating systems
(No instructions, you need to figure this out yourself then add it in here.)
Do not try to install PHP 5.4 as there is no installer and it doesn't include the PEAR installation batch file. Instead, you have to use PHP 5.3.
You probably want the thread-safe version.
While going through the installation, you probably should use all default options, except:
- There is a question about which web server to use. Since you do not have PHP installed on your system already, you probably don't want to run a web server, so choose 'Do not setup a web server'.
- There is a list of features. Expand 'Extras' and turn on 'PEAR install'.
After install completes, open a new terminal and run 'php -version' to verify that it is installed.
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.
Real operating systems
- install or enable PEAR support in your PHP, see official PEAR installation guide
- upgrade PEAR, in *nix usually
sudo pear upgrade PEAR
- 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
- Click Start button. Type 'cmd' and, instead of pressing Return, press Ctrl + Shift + Return. This should give you a popup prompt to confirm permissions, then run the command prompt as administrator.
cd "\Program Files (x86)"\php go-pear
- Hit Return repeatedly to accept all defaults.
- Accept adding junk to your registry.
- Note that when I tried this, each time you run 'pear' it will come with a messagebox popup error, but this does not seem to stop it working.
- If you use an HTTP proxy:
pear config-set http_proxy my.proxy.is:80
- Install PHPUnit and DbUnit:
pear config-set auto_discover 1 pear upgrade-all pear install pear.phpunit.de/PHPUnit pear install pear.phpunit.de/DbUnit
- Using a text editor, edit the 'phpunit' shell script in c:\Program Files (x86)\PHP. Change the first line to:
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:
cd /home/example/moodle php admin/tool/phpunit/cli/init.php
This command has to be used also after any upgrade, plugin (un)installation or adding tests to new plugin.
To execute all test suites from main configuration file execute
phpunit script from your
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
Running tests via web
Although the recommended method is the command line, for cases when you want to run tests on a remote server and do not have command-line access, you can also run tests via the web:
This is a 'secret' feature and may not be well supported.