Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
WordPress Unit Tests Quick Start

WordPress Unit Tests Quick Start Guide

This quick start guide is geared towards installing PHPUnit on OSX in order to run the WordPress unit tests. It uses homebrew to install PHP using homebrew-php. You can likely skip this step if you've already got php and pear installed properly.

If you use MAMP, then try these instructions to use MAMP's php and pear to install PHPUnit.

install homebrew

ruby <(curl -fsSkL
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

install php

brew install php54 --with-mysql --with-fpm --with-homebrew-openssl --with-imap --with-pgsql --with-intl
brew install php54-xdebug

install phpunit

sudo chmod u+w /usr/local/Cellar/php54/5.4.5/lib/php/.lock
sudo chmod g+w /usr/local/Cellar/php54/5.4.5/lib/php/.lock
pear update-channels
pear channel-discover
pear channel-discover
pear channel-discover
pear install phpunit/PHPUnit

Then add this to your $PATH: /usr/local/Cellar/php54/5.4.5/bin

checkout the test suite

svn co wp-unit-tests 
cd wp-unit-tests && svn up

create database

The test suite will destroy all data in the database, so create one dedicated to running the suite.

mysql -u root -p -e "DROP DATABASE wpunit; CREATE DATABASE wpunit; GRANT ALL PRIVILEGES ON wpunit.* TO wpunit@localhost IDENTIFIED BY 'wpunit'; FLUSH PRIVILEGES;"

configure test suite

cp wp-tests-config-sample.php wp-tests-config.php

Then edit wp-tests-config.php to connect to your database.

run the suite


run a single test

phpunit tests/query.php

Further Reading

Copy link

al-the-x commented Aug 27, 2012

Instead of installing all those channel dependencies manually, try:

pear config-set auto_discover true

Then PEAR will automatically add the appropriate channels for dependencies. A big time-saver when the package maintainer is splitting packages across channels.

Copy link

xentek commented Aug 27, 2012

@al-the-x Did that, and it didn't work. Well, it worked in getting PHPUnit code on my system but until I forced it to discover the channels on and symfony, the phpunit cli runner wouldn't install for some reason. This may have just been me, but figured I'd prove the explicit instructions so that I might avoid requests for help ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment