PHPUnit testing support in Moodle
Documentation
Installation
- install PEAR package manager - see PEAR Manual
- install PHPUnit package - see PHPUnit installation documentation
- edit main config.php - add
$CFG->phpunit_prefixand$CFG->phpunit_dataroot- see config-dist.php - execute
admin/tool/phpunit/cli/init.shto initialise the test environemnt, repeat it after every upgrade or installation of plugins
Test execution
- execute
phpunitfrom dirroot directory - you can also execute a single test
phpunit 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
testsdirectory in your plugin if does not already exist - add
*_test.phpfiles with custom class that extendsbasic_testcaseoradvanced_testcase - execute your new test, for example
phpunit local/mytest/tests/mytest_test.php
How to convert existing tests?
- create new test file in
xxx/tests/yyy_test.php - copy contents of the old test file
- replace
extends UnitTestCasewithextends basic_testcase - fix setUp(), tearDown(), asserts, etc.