Failing unit tests because of undefined functions

I recently got this error when running PHPUnit tests for one of my plugins.

Fatal error: Call to undefined function is_post_type_viewable() in /srv/www/wordpress-develop/tests/phpunit/includes/utils.php on line 365

After some searching I found out it was because the WordPress test framework used by PHPUnit is from a newer WordPress version as the WordPress version used for the tests. The undefined functions are not found because they don’t exist (yet) in core. The trac ticket dealing with this issue assumes you should only use the WP test framework from the installed branch.

Why does this happen?

For me this scenario caused the fatal error.

I’ve used the WP-CLI command wp scaffold plugin-tests to set up the unit tests for all my plugins. It generates all the files needed for running PHPUnit tests.

In bootstrap.php it tells PHPUnit where to find the WP test framework.

$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
	$_tests_dir = '/tmp/wordpress-tests-lib';

As you can see it sets the directory for the WP test framework from a environment variable WP_TESTS_DIR if it exists.

For testing locally I use Varying Vagrant Vagrants. This Vagrant configuration sets the WP_TESTS_DIR variable in its bash_profile to /srv/www/wordpress-develop/tests/phpunit/. So it points to the WP test framework from the development branch of WordPress.

Every time you provision VVV it will update the WP test framework to the newest development version. Functions that are not yet in the WordPress versions you test against could be added to it. That’s why these fatal errors will arise in future if you don’t guard against it.


There are multiple ways to solve this issue.

In the trac ticket it’s suggested you create shims for the non-existent functions. Plugins as EDD and Babble have gone this route. It means you copy and paste the functions from WordPress core in a shims.php file and wrap them in a function_exists. Then include the file in your tests bootstrap.php file so the functions exists when you run the tests.

For me it seems strange to include WordPress core functions in your plugin every time a new function is used in the WP testing framework.

Another solution is to not use the WP_TESTS_DIR environment variable as it can point PHPUnit to a wrong version of the WP testing framework. That’s why I’ve created a pull request for WP-CLI to not use the WP_TESTS_DIR anymore in its plugin scaffolding. Unfortunately it was not approved because of back compatibility. That doesn’t mean I cannot use it for my own plugins though, as I did in this commit. Now the correct version of the WP testing framework is used when running tests. Even Travis CI sees no problem in using this solution.

Download the files from this gist for your own testing environment if you prefer it over adding function shims to your plugins.

Let me know in the comments what solution you use to get rid of these errors.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s