mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
PHPUnit testing support in Moodle
Installation
- install PHPUnit PEAR extension - see PHPUnit docs for more details
- edit main config.php - add $CFG->phpunit_prefix and $CFG->phpunit_dataroot - see config-dist.php for more details
- execute
php admin/tool/phpunit/cli/util.php --install
to initialise test database - it is necessary to reinitialise the test database manually after every upgrade or installation of new plugins
Test execution
- optionally generate phpunit.xml by executing
php admin/tool/phpunit/cli/util.php --buildconfig
- it collects test cases from all plugins - execute
phpunit
shell command from dirroot directory - you can also execute a single test
phpunit core_phpunit_basic_testcase lib/tests/phpunit_test.php
- or all tests in one directory
phpunit --configuration phpunit.xml lib/tests/*_test.php
- it is possible to create custom configuration files in xml format and use
phpunit -c myconfig.xml
How to add more tests
- create
tests
directory in any plugin - add
*_test.php
files with custom class that extendsbasic_testcase
- manually add all core unit test locations to
phpunit.xml.dist
How to convert existing tests
- create new test file in
xxx/tests/yyy_test.php
- copy contents of the old test file
- replace
extends UnitTestCase
withextends basic_testcase
- fix setUp, tearDown, asserts, etc.
- some old SimpleTest tests can be executed directly - mocking, database operations, assert(), etc. does not work, you may need to add
global $CFG;
before includes
FAQs
- Why is it necessary to execute the tests from commandline? PHPUnit is designed to be executed from shell, existing Moodle globals and constants would interfere with it.
- Why
tests
subdirectory? It should not collide with any plugin name because plugin names use singular form. - Why is it necessary to include core and plugin suites in configuration files? PHPUnit does not seem to allow dynamic loading of tests from our dir structure.
TODO
- stage 2 - implement advaced_testcase - support for database modifications, object generators, automatic rollback of db, blobals and dataroot
- stage 3 - mocking and other advanced features, add support for execution of functional DB tests for different engines together (new options in phpunit.xml)
- other - support for execution of tests and cli/util.php from web UI (to be implemented via shell execution), shell script that prepares everything for the first execution