When unit testing your code, there are times when you need to test the same method or function with a variety of different parameters. For example, we have a function to check to see if a passed email is both valid and not already in the list of known emails:Continue reading “Better Unit Testing with DataProviders”
Recently I was running into issues with VVV running some units tests for AJAX. I was not able to remedy the issue so I decided to create a testing environment from the SVN repo directly.
I went to the page on the codex and while the information there was great, part of the instructions are to setup a different database for your tests but not how to get mysql setup and ready for anything to connect to it.
After a bit of digging around I found a simple approach for setting up a test suite for WordPress core unit tests. This setup does not account for viewing WordPress in a browser it was really only meant for unit testing but you could easily set that up as well if required.
Using homebrew, install mysql
brew install mysql
Once installed, start the server
Once the server has started you can login using the default user (root) and password (blank)
mysql -u root -p
You may be prompted for a password if so, just hit enter.
Now you can create the tables as needed. There are two commands here because we need a database for WordPress regular use and one for unit testing because the test suite will drop all of the tables in the database each time it’s run.
CREATE DATABASE wp-unit-tests; CREATE DATABASE wp-normal;
That’s it! Now your databases are all setup to use.
Next, checkout the WordPress repo into your user folder and change to that directory
svn co http://develop.svn.wordpress.org/trunk/ wordpress-develop $ cd wordpress-develop
Now edit the wp-config.php and wp-test-config.php adding in the appropriate database connection info for each.
I found that I needed to set the DB_HOST constant to 127.0.0.1 instead of localhost to have the connection work.
If everything is setup correctly, you can run the unit test suite from inside the root of the core repo.
Once you’re done with your testing you stop the mysql server as it can cause conflicts with other tools that use their own mysql installations.
That’s it! Happy unit testing!