Programster's Blog

Tutorials focusing on Linux, programming, and open-source

PHPUnit - Installation

Install composer if you haven't already.

Navigate to your project folder, or create a new folder for your project.

Fetch the latest stable version of phpunit through composer.

composer require "phpunit/phpunit"

Dev Only

The above command will install phpunit as a core part of your project. If you are developing something like a package, then you probably want it to only be installed if developing the package. In such case, modify your composer.json file to have it within a require-dev block like so:

    "require": {
        "php": ">=8.0.0",
    "require-dev" : {",
        "phpunit/phpunit": "^9.5"


You will want your tests to automatically be autoloaded, but probably only for dev, so be sure to add them to an autoload-dev block like so:

    "autoload": {
        "psr-4": {
            "Programster\\MyPackage\\": "src/",
            "Programster\\MyPackage\\Exceptions\\": "src/exceptions"
    "autoload-dev": {
        "psr-4": { "Programster\\MyPackage\\Tests\\": "phpunit-tests/" }

... and then just be sure to set the namespace in your test file accordingly.




namespace Programster\MyPackage\Tests;

final class TestFieldTypes extends \PHPUnit\Framework\TestCase
    public function testSomething()


To execute PHPunit from having installed through composer run the following command from where your composer.json file is.

./vendor/bin/phpunit tests/*

This will execute all the tests within the tests folder.

Optional Extras


A utility class for invoking callables with a timeout. This package is required to enforce test timeouts in strict mode.

composer require "phpunit/php-invoker"


Support database interaction testing.

composer require "phpunit/dbunit"


Selenium RC integration for PHPUnit.

composer require "phpunit/phpunit-selenium"


Last updated: 4th July 2021
First published: 16th August 2018