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:
- 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. 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.reg
file that has been created. This allows you to run thepear
command 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_discover
in 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
phpunit
command from any folder, you need to add it to your WindowsPath
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
- 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 failed 🙂It’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.exe
as 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!
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?
Does it work if you navigate to the directory where PHPUnit is installed first? If so, then you’re missing the environment variable. If not, then check your PHP error_log.
For some reason I needed to restart my computer and not just my command prompt session. It worked fine after that though I’m not really sure why.
Very helpful – thank you!
Thanks so much for this tutorial – it was a life-saver for me!
“pear” is not a command. Oh, well…
It’s interesting how whole industry cannot generate 2-pages long instruction which actually works. No wonder automated testing is so important =)
Hi Alex, Thank you so much for your tutorial. Thanks to this, I didn’t have a hard time installing phpunit. Thanks, again!
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
thanks…
how wil it install on windows 8 ?
Hi all, plz help me out in installing PHPUnit in windows7.
I have successfully installed Pear but when i run “pear install phar.phpunit.de/PHPUnit” command..it gives me below error:
Attempting to discover channel “phar.phpunit.de”…
Attempting fallback to https instead of http on channel “phar.phpunit.de”…
unknown channel “phar.phpunit.de” in “phar.phpunit.de/PHPUnit”
invalid package name/package file “phar.phpunit.de/PHPUnit”
install failed
Please help me out..i am stuck at this point 🙁
Same Problem Here:
Find solution after lot of time spend
http://jonathanmh.com/unit-testing-phpunit-windows/
You can’t install phpunit using pear anymore: https://github.com/sebastianbergmann/phpunit/wiki/End-of-Life-for-PEAR-Installation-Method
But you can still install it using the phar file: https://phpunit.de/getting-started.html
Or using composer: https://phpunit.de/manual/current/en/installation.html#installation.composer