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!

24 thoughts on “Installing PHPUnit On Windows

  1. This (and many other software) is one of the reasons I tend to use VirtualBox with a Linux environment where I could just apt-get install phpunit. I also tried installing PHPUnit on OS X a few months ago, but I failed :)

  2. Hi Alex, awesome tutorial as this is the first one I’ve attempted that actually worked!

    I notice that the wp-unittest tutorial commands always start with $ which seems to mean something… Also, the output (“S” for example) seems to be printing “color literals” into cmd.exe which I assume would be colored in a extension rather then gobbledeegook. Are you using any kind of command-line extensions, or just the vanilla windows cmd.exe?

  3. My experience of this was a little tougher but this page still helped. Note – I am using Windows 8 – and given the world seems to think that Windows 8 is the root of all evil, perhaps I deserved it… :)

    For a start, I encountered a bug when running the “pear version” command. It still ran OK, but a debug dialog popped up warning about a syntax error. The fix for that involves lining up some quotes properly in the pear.bat file – full instructions here:

    http://dnasir.com/2010/12/02/tutorial-how-to-install-pear-on-iis7/

    Then I ran through the process of installing PHPUnit and Pear indicated that all had gone well. However – when invoking the phpunit –version command at the end, it hit me with the dreaded “this isn’t a recognised program or command” that you usually get because your Path isn’t up to date. However, following the instructions above, I’d already checked the path was fine…

    So then I tried to follow the instructions from the PHPUnit site itself:

    http://phpunit.de/manual/3.0/en/installation.html

    This then hit me with a load of errors along the lines of:

    phpunit/PHPUnit requires PEAR installer version 1.9.4, installed version is 1.9.0

    (I suspect this is because I was a bit of a pussy and downloaded a slightly earlier than the latest version of PHP because there was a Windows installer, instead of manning-up and getting the raw files for the latest version).

    … so, anyway, I upgraded Pear with the command:

    pear upgrade pear

    … and then reran the command:

    pear install phpunit/PHPUnit

    … and now the phpunit –version command works.

    Phew!

    • I installed pear and phpunit seemingly without any issues on my windows 8.1 machine however when I go an type phpunit –version into the command prompt, nothing displays. No error message or anything else. Any ideas why?

    • It’s interesting how whole industry cannot generate 2-pages long instruction which actually works. No wonder automated testing is so important =)

  4. Mr Alex Mills,
    I am a newest in this kind of php,mysql and phpunit.But with your guide “Installing phpunit on windows”, I can do the installation until step 1-6 that is pear installer works correctly. At step 2-2, I don’t know: what version of phpunit and what site I can download phpunit. Please give me this information. Thank you a lot.
    Vu

Leave a Reply