Skip to content

Testing instructions

This page is intended for developers and testers.

PHPUnit tests

Creating a PHPUnit test environment

  1. Spawn a shell inside your Moodle installation root directory:
    cd /usr/share/nginx/www/moodle/
    
  2. 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';
    
  3. Download composer and install dev dependencies:
    wget https://getcomposer.org/download/latest-stable/composer.phar
    php composer.phar install
    
  4. 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:

  1. your PHPUnit test environment set up.
  2. the xdebug extension installed and enabled in your PHP environment.

Generating coverage reports

To generate code coverage reports, follow these steps:

  1. Run PHPUnit with coverage report:
    XDEBUG_MODE=coverage vendor/bin/phpunit --colors --testdox -v --coverage-html /tmp/coverage --filter "local_archiving"
    
  2. 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.