WP Austin - WordPress Unit Testing
This sample plugin contains a basic meta box for storing a byline, as well as unit tests for testing the entire plugin.
Local Setup (Mac)
- Check your PHP version in your terminal
php --version. Unit tests need PHP 7, in case you need to upgrade.
- Additional info for the following: https://foresthoffman.com/running-wordpress-phpunit-tests-with-docker/
- Make a new directory and clone the WordPress develop repo:
git clone firstname.lastname@example.org:WordPress/wordpress-develop.git
- Inside the root of the cloned wordpress-develop directory, copy the wp-tests-config-sample.php file to wp-tests-config.php and modify the DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, and WP_TESTS_DOMAIN, example:
define( 'DB_USER', 'root' ); define( 'DB_PASSWORD', 'password' ); define( 'DB_HOST', '127.0.0.1:3308' ); // Match the port number to Docker. define( 'WP_TESTS_DOMAIN', 'localhost' );
- Set the environment variable, example:
- Clone this repo locally:
git clone email@example.com:petenelson/wp-unit-tests.git
- For a database, I recommend Docker so you don't interfere with any existing database servers.
- Switch to the plugin dir and run
composer installto install PHPUnit.
- Switch to the /docker directory and run
docker-compose up -dto start the MySQL test database.
- Switch back to the main plugin dir and run
./vendor/bin/phpunitto run the unit tests.
- Bonus: run
./vendor/bin/phpunit --coverage-html test-coverage-htmlto run the unit tests with HTML test coverage in the test-coverage-htmnl directory.
- Visit travis-ci.org and sign in with your GitHub account.
- Add the repository that's holding your plugin, include a valid .travis.yml file, such as the one from this repo.
git pushnew code with unit tests
Frequently Used Assertions
I keep these handy for copy/paste.
$this->assertTrue( condition ); $this->assertFalse( condition ); $this->assertEmpty( actual ); $this->assertEquals( expected, actual ); $this->assertContains( needle, haystack ); $this->assertGreaterThan( expected, actual ); $this->assertCount( expectedCount, haystack );