How exactly *does* unit testing work for themes?

TL;DR: The only “unit” tests most themes will need are these.


There is actually a very good reason that most material is on plugin unit testing. Themes really aren’t good candidates for unit testing. A theme is usually mostly concerned with how your site is displayed, and that is something that you can’t really unit test. Yes, you can check the HTML markup generated by your theme, but the tests can’t see how your theme is displayed. Checking that your theme displays content as you want it to is something that you need to do yourself, manually.

Of course, the theme “unit” tests mentioned by @toscho will be very helpful to you in doing this. But as he pointed out, they aren’t unit tests. They merely provide some pre-written content that will let you see how your theme responds to different content that can often cause unexpected issues.


The above mentioned tests are good for all themes, and will be sufficient for most of them. However, there may still be cases in which unit tests are a good idea for a theme. These would be cases in which the theme offers other features besides just displaying content. That is, if your theme has some plugin-like attributes.

Now, be forewarned that themes probably shouldn’t offer plugin-like features in most cases. There may be times that it is needed, but you should know what you are doing and why you are doing it when you start packing this sort of functionality into a theme.

Assuming that you might be building a theme that is a case where the theme does lots more than just display content, and you are sure you need this functionality to be in the theme rather than a plugin, you’re right in wanting to create some unit tests. In that case, you are basically going to be doing the same thing as the plugin unit tests, just with a theme.

As you may have noticed in your research, there are several different approaches to doing plugin unit tests. Some want to keep their unit tests as pure as possible, so they actually don’t use WordPress at all. (See, for example, WP Mock.) I presently prefer an approach that is more like integration testing, where WordPress’s test suite is used as a starting framework for the tests. That sounds like what you are trying to get set up, and since its also what I’m most familiar with, that’s what we’ll focus on.

If you’re going to set up your tests similar to my tutorial linked above, you’ll likely just need to make a few tweaks to the parts that are specifically for plugins. I think the primary change you’ll need to make is this part:

/**
 * The path to the main file of the plugin to test.
 */
define( 'TEST_PLUGIN_FILE', '/Users/me/svn/myplguin/myplugin.php' );

/**
 * Manually load the plugin main file.
 *
 * The plugin won't be activated within the test WP environment,
 * that's why we need to load it manually.
 *
 * You will also need to perform any installation necessary after
 * loading your plugin, since it won't be installed.
 */
function _manually_load_plugin() {

    require TEST_PLUGIN_FILE;

    // Make sure plugin is installed here ...
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

You will need to load your theme’s functions.php instead of a plugin, and it’s possible that you’ll want to choose another hook that fires later than muplugins_loaded to hook your function to. (I’m not a theme expert, so I don’t know off the top of my head when WordPress loads all of the theme dependencies.)

That should give you a start, though it’s possible you’ll need to tweak some other things as well.