Skip to content

PHPUnit

Mark Metcalfe 👽 edited this page Sep 19, 2025 · 16 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
installunit

Running Tests

Run a single test file:

unit path/to/test.php

Run all tests in a directory:

unit server/totara/core

Run all tests tagged with a group:

unit --group perform

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

unit

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:

unit_pcov path/to/test.php
unit_pcov server/totara/core
unit_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