Installing PHPUnit on OS X

Your projects must be tested. It can be done manually, using PHPUnit or in another way. I'm going to show you how to set up and configure PHPUnit on OS X.


My OS is configured as described in the Configuration of OS X El Capitan blog post. Let's install phpunit using brew install phpunit. Now we can use a new command: $ phpunit. I use PhpStorm, so I want to configure autocompletion.

PhpStorm Configuration

There are a few options of how you can add phpunit to phpstorm. Let's look at them:

Using Composer

Just add a dependency to your composer.json file:

  "require-dev": {
    "phpunit/phpunit": "5.0.*"

Then run composer update phpunit/phpunit. This approach has one minus - additional code would be downloaded even if you had phpunit installed globally in your system. Let's set up a custom autoloader, so we could run PHPUnit in PHPStorm:

Using include_path

If you have PHPUnit installed globally, then the phpunit.phar file is located at /usr/local/Cellar/phpunit/5.0.0/libexec/phpunit-5.0.0.phar. Let's add that folder to our include_path setting for PHP:

If we use this approach, we will have code autocompletion, but we will still have errors if we try to run our tests via PHPStorm. We need to fix this:


As you can see, configuration of PHPUnit it's an easy walk. I also recommend reading of Enabling PHPUnit Support and Installing PHPUnit.