Testing Instructions
This page is intended for developers and testers.
PHPUnit tests
Creating a PHPUnit test environment
- Spawn a shell inside your Moodle installation root directory:
cd /usr/share/nginx/www/moodle/ - Prepare the Moodle PHPUnit configuration. Add the following lines to your
config.php:config.php<?php $CFG->phpunit_prefix = 'phpu_'; $CFG->phpunit_dataroot = '/path/to/your/phpunit_moodledata'; - Download composer and install dev dependencies:
wget https://getcomposer.org/download/latest-stable/composer.phar php composer.phar install - Bootstrap the test environment:
php admin/tool/phpunit/cli/init.php --disable-composer
See: https://moodledev.io/general/development/tools/phpunit
Running tests
After you have sucessfully created a PHPUnit envirnoment, you can run the tests using the following commands:
- Running all tests:
vendor/bin/phpunit --colors --testdox - Running all tests for a single component:
vendor/bin/phpunit --colors --testdox -v --filter "tool_userautodelete" - Running all tests for the main plugin and all sub-plugins (recommended):
vendor/bin/phpunit --configuration phpunit.xml --test-suffix _test.php --colors --testdox -v admin/tool/userautodelete -
Running a single test suite:
vendor/bin/phpunit --colors --testdox -v admin/tool/userautodelete/tests/manager_test.php -
Running data privacy compliance test suites:
vendor/bin/phpunit --colors --testdox -v --testsuite tool_dataprivacy_testsuite,tool_policy_testsuite,core_privacy_testsuite
Attention: All commands must be run from inside your Moodle root directory.
Code coverage
Prerequisites
To generate code coverage reports, you need to have:
- your PHPUnit test environment set up.
- the
xdebugextension installed and enabled in your PHP environment.
Generating coverage reports
To generate code coverage reports, follow these steps:
- Run PHPUnit with coverage report:
XDEBUG_MODE=coverage vendor/bin/phpunit --configuration phpunit.xml --test-suffix _test.php --colors --testdox -v --coverage-html /tmp/coverage admin/tool/userautodelete - Open the report in your browser:
xdg-open /tmp/coverage/index.html
Attention: It can be required to purge your local /tmp/covarage directory between consecutive runs. If you find
changes not being reflected correctly in the report, try to delete the /tmp/coverage directory before running the unit
tests again.