PHPUnit: Difference between revisions
Line 58: | Line 58: | ||
=Writing new tests= | =Writing new tests= | ||
* | * read [http://www.phpunit.de/manual/current/en/ official PHPUnit online documentation] | ||
* see [[Writing unit tests]] | |||
=Conversion of existing SimpleTests= | =Conversion of existing SimpleTests= |
Revision as of 20:03, 23 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. Moodle requires PHPUnit version 3.6.0 or higher.
- install or enable PEAR support in your PHP (PHPUnit requires PEAR Installer version >= 1.9.4 and "pcntl" and "pdo" PHP extensions)
- upgrade PEAR as root or administrator:
pear upgrade
- install PHPUnit package as root or administrator:
pear config-set auto_discover 1
or
pear channel-discover pear.phpunit.de
and then
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.
Writing new tests
Conversion of existing SimpleTests
See SimpleTest conversion page.