Running Unit tests

Install PHPUnit and the TYPO3 testing framework

In order to run unit tests within an extension or project you can require PHPUnit and the testing framework via Composer as a development dependency:

composer require --dev \
  "typo3/testing-framework":"^8.0.9" \
  "phpunit/phpunit":"^10.5"
Copied!

Which versions to use depends on the PHP and TYPO3 versions to be supported.

The following matrix can help you to choose the correct versions.

testing-framework TYPO3 PHP PHPUnit
8.x.x v12, v13 (main) 8.1, 8.2, 8.3 (8.4) ^10, ^11
7.x.x v11, v12 7.4, 8.0, 8.1, 8.2, 8.3 (8.4) ^9, ^10
6.x.x v10, v11 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3 ^8, ^9

Testing framework <= 6.x is no longer maintained.

Provide configuration files for unit tests

The TYPO3 testing framework comes with a predefined unit test configuration and a bootstrapping file. You should copy these files into your project so you can adjust them as needed:

Copy the files vendor/typo3/testing-framework/Resources/Core/Build/UnitTests.xml and vendor/typo3/testing-framework/Resources/Core/Build/UnitTestsBootstrap.php

Open file UnitTests.xml and adjust the paths to the path (or multiple paths) where the unit tests are stored. By convention many extensions store them in the directory Tests/Unit and subdirectories thereof:

UnitTests.xml for extension testing
<testsuites>
    <testsuite name="Unit tests">
-        <directory>../../../../../../typo3/sysext/*/Tests/Unit/</directory>
+        <directory>../../Tests/Unit/</directory>
    </testsuite>
</testsuites>
Copied!

If you are testing in a project you will probably have a directory from where local extensions like site packages and client-specific extensions are installed , so you can also include them:

UnitTests.xml for project testing
<testsuites>
    <testsuite name="Unit tests">
-        <directory>../../../../../../typo3/sysext/*/Tests/Unit/</directory>
+        <directory>../../Tests/Unit/</directory>
+        <directory>../../packages/*/Tests/Unit/</directory>
    </testsuite>
</testsuites>
Copied!

Run the unit tests on your system or with DDEV

If you have the required PHP version installed on your host system, you can run the unit tests directly:

php vendor/bin/phpunit -c Build/phpunit/UnitTests.xml
Copied!

Or you can run them on ddev:

ddev exec php vendor/bin/phpunit -c Build/phpunit/UnitTests.xml
Copied!

If you are using DDEV, you can also create a custom command so you can run these tests again and again.

If you want to only run a specific test case (test class) or test (method in a test class) you can use the filter option:

php vendor/bin/phpunit -c Build/phpunit/UnitTests.xml --filter "MyTest"
Copied!

You can of course define a Composer script <https://getcomposer.org/doc/articles/scripts.md> as well, so that this command can be executed easily on the host, within a DDEV container and also in GitHub Actions or Gitlab CI.

Run the unit tests with runTests.sh

If you are using a runTests.sh like the one used by the t3docs/blog-example you can run the tests with:

Build/Script/runTests.sh -s unit
Copied!

It is also possible to choose the PHP version to run the tests with:

Build/Script/runTests.sh -s unit -p 8.2
Copied!

You can start by copying the runTests.sh of blog_example and adjust it to your needs.

There are different solutions to store and execute these commands. For details see Test Runners: Organize and execute tests.

runTests.sh is a script that originates from the TYPO3 Core repository and is used as a test and tool execution runner. It is based on running individual Docker containers with several bash commands, and also allows Xdebug integration, different database environments and much more. Once you copy such a file to your repository you need to take care of maintaining it when possible bugfixes or changes occur upstream.