As the majority of material on unit testing focuses on plugin development rather than theme development, I've hit a stumbling block when trying it out for myself. I've installed PHPUnit, grabbed the test suite from the WP repo, and configure it so that the tests run. What I haven't quite figured out is how do you specify which theme it should test? I've gathered that as part of the testing process it actually creates a separate install of WordPress, but at what point do you tell it to use a particular theme? Am I missing something obvious?

  • 6
    The theme unit tests from wordpress.org are not unit tests. Whoever gave it that name had no idea what unit tests are.
    – fuxia
    Dec 7, 2014 at 18:41
  • If you are still interested in "automated testing" for a theme's look, perhaps you might want to look into "end-to-end" testing. This is where you test a website's look and standard procedures as an end user. It's normally done for web applications for testing buttons and controls, but this could also work out for static sites. Or you could check for css classes if you do go with standard unit testing, but honestly, that's such a hastle to set up just to check for styles. Manual testing is just much faster than those options. Apr 30, 2019 at 20:13

1 Answer 1


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.

  • Wonderful answer; I have a much clearer understanding now. I understand that in principle themes have limited functionality and as such aren't really candidates for unit testing; however, I'm going to persevere and attempting to setup unit tests for my functions.php purely as an educational exercise.
    – Dre
    Dec 8, 2014 at 16:48
  • Hey Dr @Dre , did you figure out how to add UTs to functions.php? I'm kind of having to do the same thing without success. Did you do it?
    – Absulit
    Nov 19, 2019 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.