Note:

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

PHPUnit: Difference between revisions

From MoodleDocs
No edit summary
Line 1: Line 1:
{{Moodle 2.3}}
{{Moodle 2.3}}
=What is PHPUnit=
PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP, it is distributed as PEAR package and is not part of Moodle installation. You have to manually install it on your development computer or test server.


=Installation of PHPUnit support=
=Installation of PHPUnit support=


PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP, it is distributed as PEAR package and is not part of Moodle installation. You have to manually install it on your development computer or test server. The installation procedure depends on your operating system or the way you installed PHP, minimum required version is 3.6.0.
The installation procedure depends on your operating system or the way you installed PHP, minimum required version is 3.6.0.


Installation steps:
Installation steps:

Revision as of 19:03, 20 April 2012

Moodle 2.3


What is PHPUnit

PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP, it is distributed as PEAR package and is not part of Moodle installation. You have to manually install it on your development computer or test server.

Installation of PHPUnit support

The installation procedure depends on your operating system or the way you installed PHP, minimum required version is 3.6.0.

Installation steps:

  1. install or enable PEAR support in your PHP
  2. upgrade PEAR as root or administrator: pear upgrade PEAR
  3. install PHPUnit package as root or administrator:
pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
pear install phpunit/DbUnit

Detailed instructions:

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.

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.


There is an alternative script for running of tests via web interface admin/tool/phpunit/webrunner.php, use it as the last resort only if you can not use command-line interface on your test server. It will most probably break due to permissions problems if you try to execute it both from command-line and from webrunner. This feature is not officially supported.

PHPUnit support in IDEs

What next?