3.0.1 2017-09-26 05:27 UTC


Example of an extension for PHPUnit.



If you use Composer to manage the dependencies of your project then you can add the PHPUnit example extension as a development-time dependency to your project:

$ composer require --dev phpunit/example-extension

PHP Archive (PHAR)

If you use PHPUnit 5.7 (or later) from a PHP Archive (PHAR) then you can download a PHAR of the PHPUnit example extension:

$ wget

The example below shows how to configure PHPUnit to load all *.phar files found in a given directory (tools/phpunit.d in this example):

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi=""

PHPUnit will only load a an extension PHAR if it provides valid manifest information in a manifest.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<phar xmlns="">
    <contains name="phpunit/phpunit-example-extension" version="3.0.0" type="extension">
        <extension for="phpunit/phpunit" compatible="^6.3"/>

        <author name="Sebastian Bergmann" email=""/>
        <license type="BSD-3-Clause" url=""/>

        <php version="^7.1"/>

Of course, the extension to be loaded must also be compatible with the version of PHPUnit trying to load it. The extension provides the information required for this compatbility check in its manifest.

When verbose output is activated, PHPUnit will print loaded extension PHARs:

$ phpunit --verbose tests
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.1.8 with Xdebug 2.5.5
Configuration: /home/sb/example/phpunit.xml
Extension:     phpunit/phpunit-example-extension 3.0.0

.                                                                   1 / 1 (100%)

Time: 32 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

The --no-extensions commandline option can be used to suppress the loading of extensions from the directory configured using extensionsDirectory:

$ phpunit --no-extensions
PHP Fatal error:  Trait 'PHPUnit\ExampleExtension\TestCaseTrait' not found in ...