WordPress testing (core) is useful when contributing to the WordPress core. The first thing you have to do is install PHPUnit 7 (8 doesn't work with WordPress yet).
In your terminal, run
curl https://phar.phpunit.de/phpunit.phar -L -o phpunit.phar chmod +x phpunit.phar mv phpunit.phar /usr/local/bin/phpunit
You can also run
brew install phpunit chmod +x phpunit.phar mv phpunit.phar /usr/local/bin/phpunit
Then check the test repository. WordPress tests live in the core development repository, available via SVN at:
svn co https://develop.svn.wordpress.org/trunk/ wordpress-develop cd wordpress-develop
git clone git://develop.git.wordpress.org/ wordpress-develop cd wordpress-develop
Create an empty MySQL database. Be careful not to use an existing project database, as the test suite will delete all data from the designated database.
Set up a config file. Copy
wp-tests-config.php and enter the credentials of the newly created database.
Running the test suite
To run unit tests, you need to specify the
phpunit.xml.dist file. An example might look like this:
<?xml version="1.0"?> <phpunit bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" > <testsuites> <testsuite> <directory prefix="test-" suffix=".php">./tests/</directory> </testsuite> </testsuites> <filter> <whitelist> <directory>./</directory> <exclude> <directory>./vendor</directory> <directory>./tests</directory> </exclude> </whitelist> </filter> <logging> <log type="coverage-clover" target="tests/_reports/logs/clover.xml"/> <log type="coverage-html" target="tests/_reports/coverage" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" /> <log type="testdox-text" target="tests/_reports/testdox/executed.txt"/> <log type="testdox-html" target="tests/_reports/testdox/testdox.html" /> </logging> </phpunit>
Here we've specified that code coverage should be created. For that to be done, you need to have Xdebug installed on your system. Keep in mind that Xdebug may significantly reduce the speed of your tests.
In the root directory, where
wp-tests-config.php file, the
tests/ folder, and the
phpunit.xml.dist file are located, run:
Unit test symbol meaning
- . – Each dot signifies one “test” that passed.
- S means a test was skipped. This usually means that the test is only valid in certain configurations, such as when a test requires Multisite.
- F means a test failed. More output on what exactly failed and where will be shown.
- E means a test failed because of a PHP error, warning, or notice.
- I means a test was marked as incomplete.
Testing inside VVV
VVV comes with a test-ready environment
wordpress-develop/, where you can test various patches. It already has the test suite set up so you don't need to do anything manually.
Once you are in the
wordpress-develop/public_html/ folder, run
npm install -g grunt-cli
grunt-cli has been installed, run
If the build fails on the
node-sass package, try rebuilding it again.
npm rebuild node-sass
Note: The build process is currently under revision (https://core.trac.wordpress.org/ticket/43055?cversion=0&cnum_hist=23).
Applying a patch
Make sure that VVV is running and the above steps have been completed. You can then ssh to Vagrant, go to the
wordpress-develop/public_html/ folder and run
grunt watch & svn up
Now you’re ready to patch. Find a ticket with patches. This example uses
This will show the available patches you can choose from (if there are multiple patches). After you select the patch, you can log into
http://src.wordpress-develop.test/). You can test the effects of that patch and, after you're done with it, you can revert it using
svn revert -R
svn revert -R --cl revertme .
You can comment on the Trac ticket with the findings or create you own patch.
Testing outside wordpress-develop
You can set up a custom clean installation of WordPress if you don't want to use the official one from VVV. In that case, just go to public_html and run
svn co https://develop.svn.wordpress.org/trunk/ .
This will download all files that are necessary for testing and patching, as described above.