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:
- Install PEAR, a dependency for PHPUnit:
- 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. - Open an administrator command prompt. On Vista or Windows 7, 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.
- Type the following command to execute the file you just downloaded:
php go-pear.phar - 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.
- Open the folder where PHP is installed and double-click the
PEAR_ENV.regfile that has been created. This allows you to run thepearcommand from any folder. - Verify PEAR is working by running the command
pear version
- 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
- Install PHPUnit:
- Turn on
auto_discoverin PEAR by typing the following command at the command line:pear config-set auto_discover 1 - Download and install PHPUnit by running the following command:
pear install pear.phpunit.de/PHPUnit - In order to be able to run the
phpunitcommand from any folder, you need to add it to your WindowsPathvalue. 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 - Verify PHPUnit is working by running the command
phpunit --version
- Turn on
- 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!
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 failedIt’s certainly easier on Linux but that requires that you are actually comfortable using a Linux environment which I am not.
If you install PEAR using an admin command line, it should set the PATH automatically for you.
I think I tried doing that while having started up
cmd.exeas an administrator but I’m not 100% sure. You may be right.Regardless, double-clicking a registry file is easy.
I just tried the tutorial without admin priv on the cmd, (for php go-pear.phar) and it didn’t work well: pear config-show displays lots of C:\Windows locations (a popular bug). Running as admin changed all the locations to the php folder. Worth mentioning in the tut?
I think it depends where you have PHP installed but I’ve gone ahead and done that as it certainly can’t hurt to run it as an administrator.
What tool do you use to run unit tests? Command line?
Yep.
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?
found: turning off “color” in phpunit.xml fixes the issue, but only until svn update I suppose
When you
svn up, it should keep your local mod in place. Also thanks for this solution!$is just the symbol shown on a Linux command line prompt, like so:It’s just a way of showing that line is a command that was run rather than output from a script.
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!