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
Line 38: Line 38:


=Test execution=
=Test execution=
PHPUnit tests are executed from the command line. PHP IDEs usually have integration options for PHPUnit.


To execute all system test suites from main configuration file execute phpunit script from dirroot directory:
To execute all system test suites from main configuration file execute phpunit script from dirroot directory:

Revision as of 13:07, 8 April 2012

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


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

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 system test suites from main configuration file execute phpunit script from dirroot directory:

cd /home/example/moodle
phpunit

To execute individual test file use the relative path to the test file:

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

To execute all tests from one directory run:

cd /home/example/moodle
phpunit lib/tests/*_test.php

If you execute phpunit form directory different from your $CFG->dirroot you need to specify path to configuration file.

cd ~/
phpunit -c /home/example/moodle/phpunit.xml /home/example/moodle/lib/tests/phpunit_test.php

In IDEs you may need to specify path to configuration file, use "/home/example/moodle/phpunit.xml".

PHPUnit support in IDEs

See also