Skip to content

PHPUnit

Mark Metcalfe 👽 edited this page Nov 21, 2025 · 17 revisions

Installing PHPUnit

Log into one of the PHP containers:

# must run the command from the root of your Totara repo
cd sitefolder
tzsh php-8.3 # or any other php container

Initiate the PHPUnit setup:

# See shell/phpunit-aliases.sh for what this alias does
install_phpunit

Running Tests

Run a single test file:

phpunit path/to/test.php

Run all tests in a directory:

phpunit server/totara/core

Run all tests tagged with a group:

phpunit --group perform

Run all tests in the site - this may take hours!

phpunit

IDE Integration

It is possible to run tests directly inside PHPStorm/IntelliJ and Visual Studio Code too.

Code Coverage

Note: This will only work with Totara 14+

PCOV is available for collecting code coverage and generating reports.

To set the directories to cover with code coverage, you can use the set_coverage command from within the PHP container shell.

For example, if you wanted to collect coverage for the lib and totara core directories, you would run:

set_coverage server/lib server/totara/core

To then run PHPUnit with coverage, you can run unit_pcov in the same way you'd run unit.

For example:

phpunit_pcov path/to/test.php
phpunit_pcov server/totara/core
phpunit_pcov --group perform

Which would then generate a coverage report viewable at http://coverage-report.localhost

To clear code coverage directories, run clear_coverage

IDE Integration

It is possible to collect coverage directly inside PHPStorm/IntelliJ too.

Clone this wiki locally