Unit Test Hack Day Prep
On Saturday, April 27th, PDXWP is hosting a hack day focusing on unit testing. Max Cutler, who has contributed significantly to WordPress core unit tests, will be teaching the group about unit testing.
WordPress core unit tests are written using PHPUnit. While PHPUnit is not the only unit testing framework for PHP, it is what WordPress has adopted and therefore, our hack day will focus on PHPUnit.
One of the most frustrating aspects of PHPUnit is getting it set up in the first place. Ideally, we will spend our time on Saturday learning to write unit tests, discussing how to improve unit tests, and getting our projects to pass the tests. Hopefully, we will not spend our time trying to get PHPUnit configured.
To this end, I have stood on the giant shoulders of Jeremy Felt and 10up by adding PHPUnit and the WordPress core unit tests to their outstanding Varying Vagrant Vagrants (VVV) vagrant setup. Instead of painstakingly writing a tutorial on how to install PHPUnit on OS X, Linux and Windows (and then trying to work around difficulties with MAMP, WAMP, XAMPP, etc.), this vagrant setup will build a virtual environment for you to work with locally during the dev day.
- Install Virtual Box .
- Install Vagrant.
- Add “192.168.50.4 local.wordpress.dev local.wordpress-trunk.dev” to your hosts file.
- Determine where you want to store your vagrant files. I store mine in “~/Vagrant” on OS X.
- Clone VVV to your hard drive with, “git clone email@example.com:10up/varying-vagrant-vagrants.git”.
- Move to the cloned directory (e.g., “cd varying-vagrant-vagrants”).
- Run “vagrant up”.
You will now see a lot of output from your terminal. The base box will be downloaded, the server will be created, and the provisioning script will be executed, which installs a LEMP stack with a WordPress installation and the core unit tests. This can take between 10-30 minutes and is highly dependent upon your download speed (read: don’t try this at Starbucks or on the hack day!).
To test if you have correctly setup the box, visit “http://local.wordpress-trunk.dev” in your browser. If everything installed properly, you should see an installation of WordPress with the Twenty Thirteen theme.
To learn more about this vagrant setup, you can read the box’s description read me.
Running Unit Tests
- In terminal, navigate to your clone of the VVV repo.
- Run “vagrant ssh”. You are now ssh-ed into the virtual machine.
- Run “cd /srv/www/wordpress-unit-tests”.
- Run “sudo phpunit”
You should now see a lot of output:
You are seeing the WordPress unit tests running! In order to not spoil the fun, I will let Max explain what is going on during our meetup!
Your job is simple–get PHPUnit working before the hack day. We will all have a lot more fun if everyone is already setup in advance. We can then focus on writing tests instead of setup up the tool. If you have questions, please have at it below! We will be happy to help.