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
m (Minor grammatical changes)
Line 2: Line 2:


=What is PHPUnit=
=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.
PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP. It is distributed as a PEAR package 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 support=
=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.
The installation procedure depends on your operating system and 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)
# Install or enable PEAR support in your PHP installation (PHPUnit requires PEAR Installer version >= 1.9.4 and "pcntl" and "pdo" PHP extensions).
# upgrade PEAR as root or administrator:  <code>pear upgrade</code>
# Upgrade PEAR as root or administrator:  <code>pear upgrade</code>
# install PHPUnit package as root or administrator:
# Install the PHPUnit package as root or administrator:
  pear config-set auto_discover 1
  pear config-set auto_discover 1


Line 28: Line 28:
=Initialisation of test environment=
=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
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_prefix = 'phpu_';
  $CFG->phpunit_dataroot = '/home/example/phpu_moodledata';
  $CFG->phpunit_dataroot = '/home/example/phpu_moodledata';


Then you need to initialise the test environment using following command:
Then you need to initialise the test environment using following command.


  cd /home/example/moodle
  cd /home/example/moodle
  php admin/tool/phpunit/cli/init.php
  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.
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=
=Test execution=


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


  cd /home/example/moodle
  cd /home/example/moodle
  phpunit
  phpunit


Use relative path to the test file for execution of one test only (test case classname followed by path to test file):
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
  cd /home/example/moodle
  phpunit core_phpunit_basic_testcase lib/tests/phpunit_test.php
  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 <code>phpunit.xml</code> from your <code>$CFG->dirroot</code>.
In IDEs, you may need to specify the path to the PHPUnit configuration file. Use the absolute path to <code>phpunit.xml</code> from your <code>$CFG->dirroot</code>.




There is an alternative script for running of tests via web interface <code>admin/tool/phpunit/webrunner.php</code>, 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.
There is an alternative script for running of tests via web interface: <code>admin/tool/phpunit/webrunner.php</code>. 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.


=Writing new tests=
=Writing new tests=

Revision as of 07:37, 24 April 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 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 support

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';

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

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.

Writing new tests

Conversion of existing SimpleTests

PHPUnit support in IDEs