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 successfully 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 "local_archiving"
-
Running a single test suite:
vendor/bin/phpunit --colors --testdox -v local/archiving/tests/archive_job_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.
Debugging tests
From time to time it can be useful to attach XDebug during a test run. To do this, you can activate XDebug by passing the respective environment variable(s) during the PHPUnit runner invocation.
Example:
XDEBUG_CONFIG="idekey=PHPSTORM" XDEBUG_MODE=debug vendor/bin/phpunit --colors --testdox -v --filter "local_archiving"
Code coverage
Prerequisites
To generate code coverage reports, you need to have:
- your PHPUnit test environment set up.
- the
xdebug
extension 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 --colors --testdox -v --coverage-html /tmp/coverage --filter "local_archiving"
- 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.