I’ve begun using PHPUnit, WordPress, and MAMP to introduce unit testing to my theme and plugin development. Obviously, this requires an installation of PHPUnit and the WordPress testing framework. Because I use MAMP for local development, I had to do some additional customization to get the frameworks setup.

Here are the steps necessary to install PHPUnit and the WordPress Tests on Mac OS X using MAMP:

Install PHPUnit

To install PHPUnit, you’ll need to use Pear. It’s included with MAMP, but you’ll likely need to update it. In a terminal session, issue the following command:

$ cd /Applications/MAMP/bin/php/php5.3.6/bin
$ sudo ./pear upgrade pear

In the comments, Japh noted that the received an error when trying to run Pear. If you experience this, run the following command:

$ mv /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf.old

The problem is that Pear’s configuration file is out of date – this just moves it to a file with a different name.

Next, tell Pear to use repositories that we specify in the command line:

$ sudo /Applications/MAMP/bin/php/php5.3.6/bin/pear config-set auto_discover 1

Finally, install PHPUnit:

$ sudo /Applications/MAMP/bin/php/php5.3.6/bin/pear install pear.phpunit.de/PHPUnit

If you’re interested in creating a symbolic link to the pear installation, run the following command. This will allow you to run ‘phpunit’ in any terminal session:

$ sudo ln -s /Applications/MAMP/bin/php/php5.3.6/bin/phpunit /usr/local/bin/phpunit

Install The WordPress Tests

Installing the WordPress Tests is relatively easy. You can grab a copy of the code from the Subversion repository and either clone or extract a downloaded archive. The easiest way to setup the rests is to drop the wordpress-tests directory in the root of the WordPress installation directory.

After that, update the wp-tests-config-sample.php just as you would a standard wp-config.php file. Rename it to wp-tests-config.php.

Make sure that you use a database that you don’t mind destroying as the tests will create and empty tables during the course of their execution.

Depending on where you’ve installed the WordPress Tests, you may need to update the path to WordPress. For example, if they are running in the root of the WordPress installation then you’re good to go, but if you have them running in, say, a theme directory, you’ll need to locate the following line in wp-tests-config.php:

define( 'ABSPATH', dirname( __FILE__ ) . '/wordpress/' );

And change it to:

define( 'ABSPATH', '../../../../' );

Once done, you can begin issuing commands to execute the WordPress tests. Assuming that you’ve setup the symbolic link to PHPUnit as covered above, you can test, for example, the user capabilities by running:

$ phpunit tests/test_user_capabilities.php

If you happen to get an error, it’s likely because the WordPress Tests attempt to use the mysql socket located in /var/mysql on the local machine, but if you’re using MAMP, then the location of the socket doesn’t exist.

To fix this, create a symbolic link between the socket in /var/mysql to the socket provided my MAMP:

$ sudo mkdir /var/mysql
$ sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

After that, you should be good to go.


Join the conversation! 16 Comments

  1. Nice post, Tom! I haven’t set this up with MAMP yet, I’ll give it a go tonight :)

  2. Damn, stuck on the pear upgrade with a nasty “unserialize()” error… hopefully that’s something unique to my system!

  3. I’ve found that pear is easier to work w/ on a virtualbox/vagrant setup. You can match your production environment better then too!

  4. You should be aware that that GitHub repository is not the official unit test suite. You should find that at http://unit-tests.trac.wordpress.org/ (SVN repository: http://unit-test.svn.wordpress.org/trunk/)

    When using the official suite, you can just run ‘phpunit’ (without the ‘all’).

  5. I had an issue with the mysql socket thing and i’ve fixed it with your solution, thanks :)

  6. I just wanted to say thanks. This post was a great help in getting everything setup on my local machine.

  7. Would love to see an update to this … the last step of installing phpunit fails in yosimite with mamp 3.0.3

  8. Here is very nice script i build that will handle that and much much it offers two solutions a terminal script app and a web app. Both will get latest wordpress and installed it and configure mamp to map and create your lcoal host here is the link https://github.com/michelve/wordpress-mamp-localhost-generator

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.