Installing PHPUnit On Windows

READ THIS: This guide no longer works due to PHPUnit dropping support for the PEAR installation method. If you’re at this page in order to use PHPUnit for WordPress-related things, then these days I recommend setting up an instance of Varying Vagrant Vagrants which is a pre-built virtual machine configuration for developing using WordPress and includes PHPUnit pre-configured. Otherwise check out the official PHPUnit installation documentation on how to install PHPUnit globally.

I wanted to start contributing to the WordPress unit tests so I needed to install PHPUnit. Turned out it was harder than it might seem (I had a tough time getting it all working) so I thought I’d blog what finally ended up working for me to help save some people some time.

Assuming you already have PHP and MySQL installed, here’s the steps you need to take:

  1. Install PEAR, a dependency for PHPUnit:
    1. Visit http://pear.php.net/go-pear.phar in your browser and save the file into your PHP directory. This is the folder where you can find php.exe.
    2. Open an administrator command prompt. Hit your Windows key, type “cmd”, right-click the resulting “cmd.exe” search result, and select “Run as administrator”. Navigate to the folder where you have PHP installed, the same folder where you saved the file in the previous step.
    3. Type the following command to execute the file you just downloaded: php go-pear.phar
    4. After a moment, you should start being prompted for some things. The installer is pretty self-explanatory and I think you want a system installation rather than a local one.
    5. Open the folder where PHP is installed and double-click the PEAR_ENV.reg file that has been created. This allows you to run the pear command from any folder.
    6. Verify PEAR is working by running the command pear version
  2. Install PHPUnit:
    1. Turn on auto_discover in PEAR by typing the following command at the command line: pear config-set auto_discover 1
    2. Download and install PHPUnit by running the following command: pear install pear.phpunit.de/PHPUnit
    3. In order to be able to run the phpunit command from any folder, you need to add it to your Windows Path value. Right-click My Computer → Properties → Advanced system settings → Environmental Variables → select “Path” under “System Variables” → Edit → Add a semi-colon (;) and then the full path to your PHP folder onto the end of the value, for example like this: ;D:\Webserver\php
    4. Verify PHPUnit is working by running the command phpunit --version
  3. Set up the WordPress unit tests by following the rest of the steps on the WordPress Core Contributor Handbook now that you have PHPUnit installed.

Done!