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 72: Line 72:
To execute all test suites from main configuration file execute the <code>vendor/bin/phpunit</code> script from your <code>$CFG->dirroot</code> directory.
To execute all test suites from main configuration file execute the <code>vendor/bin/phpunit</code> script from your <code>$CFG->dirroot</code> directory.


  $ cd /home/example/moodle
  cd /home/example/moodle
  $ vendor/bin/phpunit
  vendor/bin/phpunit


The rest of examples uses <code>phpunit</code>, please substitute it with <code>vendor/bin/phpunit</code> or create a shortcut in your dirroot.
The rest of examples uses <code>phpunit</code>, please substitute it with <code>vendor/bin/phpunit</code> or create a shortcut in your dirroot.

Revision as of 14:51, 25 November 2012

Moodle 2.3


What is PHPUnit

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

Installation of PHPUnit via Composer (recommended)

Moodle 2.4

The installation procedure depends on your operating system.

  • Install Composer dependency manager
$ cd /your/moodle/dirroot
$ curl -s https://getcomposer.org/installer | php
  • execute Composer installer
$ cd /your/moodle/dirroot
$ php composer.phar install

Detailed instructions:

Installation of PHPUnit via PEAR

The installation procedure depends on your operating system and the way you installed PHP. Moodle requires PHPUnit version 3.6.0 or higher.

  1. Install or enable PEAR support in your PHP installation (PHPUnit requires PEAR Installer version >= 1.9.4 and "pcntl" and "pdo" PHP extensions).
  2. Upgrade PEAR as root or administrator: pear upgrade
  3. Install the 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 a 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';

If your system does not have a direct connection to the Internet, you also need to specify your proxy in config.php - even though you normally specify it by using the admin settings user interface. (If you do not use a proxy, you can skip this step.) Check the settings on the relevant admin settings page, or from the mdl_config table in your database, if you are unsure of the correct values.

// Normal proxy settings
$CFG->proxyhost = 'wwwcache.example.org';
$CFG->proxyport = 80;
$CFG->proxytype = 'HTTP';
$CFG->proxybypass = 'localhost, 127.0.0.1, .example.org';
// Omit the next lines if your proxy doesn't need a username/password:
$CFG->proxyuser = 'systemusername';
$CFG->proxypassword = 'systempassword';

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 repeated after any upgrade, plugin (un)installation or if you have added tests to a plugin you are developing:

Test execution via Composer

Moodle 2.4

To execute all test suites from main configuration file execute the vendor/bin/phpunit script from your $CFG->dirroot directory.

cd /home/example/moodle
vendor/bin/phpunit

The rest of examples uses phpunit, please substitute it with vendor/bin/phpunit or create a shortcut in your dirroot.

Test execution via PEAR

To execute all test suites from main configuration file execute the phpunit script from your $CFG->dirroot directory.

cd /home/example/moodle
phpunit

Use the relative path to a test file when you wish to execute a single 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 the path to the PHPUnit configuration file. Use the 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 this as the last resort only when you cannot use the 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.


How to run only some tests

Running a single test quickly

The fastest way to run a single test is:

phpunit my_test_class_name my/tests/filename.php

This is faster than other methods because it avoids the need to search for the test file, but you should be careful because it may be possible to run tests this way which are not included in the normal run. If you use this method, do at least one full test run (or --group run, as below) to ensure the test can be found.

Using the @group annotation

If you add annotations like

/**

* Unit tests for {@link stack_cas_keyval}.
* @group qtype_stack
*/

class stack_cas_keyval_exception_test extends basic_testcase {

to all the classes in your plugin, the you can run just the tests for your plugin by doing

phpunit --group qtype_stack

Therefore, it is suggested that you annotate all your tests with the Frankenstyle name of your plugin.

Using multiple phpunit.xml files

It's easy to create alternative phpunit.xml files defining which tests must be run together. For reference, take a look to the default /phpunit.xml available in your base directory once the testing environment has been initialised. After creating the custom file you will be able to run those tests with

phpunit -c path/to/your/alternative/phpunit/file.xml

Also, for commodity, you can use this command:

php admin/tool/phpunit/cli/util.php --buildcomponentconfigs

It will, automatically, create one valid phpunit.xml file within each component (plugin or subsystem) and other important directories, so later you will be able to execute tests like

phpunit -c mod/forum[/phpunit.xml]  // Note that it's not needed to specify the name of the file (if it is 'phpunit.xml').
phpunit -c question
phpunit -c question/type/calculated
phpunit -c backup
phpunit -c lib/dml
...

or, also

cd directory/with/phpunit.xml
phpunit

Writing new tests

Conversion of existing SimpleTests

PHPUnit support in IDEs

Common Unit Test Problems

Common unit test problems