This Theme testing step corresponds to a WordPress export (WXR) file that you can import into a WordPress installation to test your Theme. To be clear, this is only one step in the process of testing your Theme. For a complete guide, see Theme testing.
Test Environment Setup
Clone the theme test data file
themeunittestdata.wordpress.xml from the GitHub repository https://github.com/WPTT/theme-unit-test (last updated 30/January/2020).
Or download a copy from https://raw.githubusercontent.com/WPTT/theme-unit-test/master/themeunittestdata.wordpress.xml
- Import test data into your WordPress install by going to Tools => Import => WordPress
- Select the XML file from your computer
- Click on “Upload file and import”.
- Check the “Download and import file attachments” box under “Import Attachments,” then click submit..
Note: To get the full list of Posts and Media, you may need to repeat the Import step until you see “All Done.”
- Read Becoming a reviewer, and follow the easy steps.
Theme Testing Process
- Resolve PHP and WordPress errors. To see deprecated function calls and other WordPress-related errors, add the following debug setting to your wp-config.php file: define(‘WP_DEBUG’, true);. See Deprecated Functions Hook for more information.
- Check template files against Template File Checklist (see above).
- Do a run-through using the Theme Unit Test.
- Validate HTML and CSS. See Validating a Website.
- Test in all your target browsers. For example, IE9, Safari, Chrome, Opera, Firefox and Microsoft Edge.
- Clean up any extraneous comments, debug settings, or TODO items.
- See Theme Review if you are publicly releasing the Theme by submitting it to the Themes Directory.
Understanding How Themes Are Different Than Plugins
Before we begin creating a theme or reviewing any code, it’s critical to understand the distinction between theme and plugin development. To begin, plugins can be written in one of two ways:
- As a collection of functions encapsulated in a single object (which is what we did in this article).
- As a straightforward set of functions
Both methods accomplish the same goal: they use a collection of functions and filters to add new functionality to WordPress. The primary distinction is in the manner in which the functions are encapsulated.
When it comes to theme development, however, there is really only one way to go, and that is to use a collection of functions defined in functions.php. This presents two challenges when writing unit tests for themes:
- Because the theme isn’t object-oriented, we can’t store the object in an array like we did in the previous article.
- We must figure out how to write and test our theme’s functions so that they can run independently of the theme being loaded into a browser.
Because good themes use a collection of filters and actions, we’ll be creating a theme that adheres to those best practices, and because the focus of this article is on unit testing themes, we’ll be focusing more on writing the tests rather than creating a good-looking, highly functional theme.
Preparing to Unit Test
Before we begin coding, let’s set up our project’s directory. We need to set up the theme’s skeleton, so create a new directory for the theme in your WordPress theme directory. Basic-Theme is my name of mine. Fill in the blanks with the following files (we’ll finish them later):
Let’s stub out the stylesheet so that WordPress recognizes it and allows us to activate it from the Dashboard. Add the following code to accomplish this:
/* Theme Name: Basic Theme Theme URI: https://themedev.net Version: 1.0 Description: A basic theme used to demonstrate how to write unit tests for themes. Author: ThemeDev Author URI: https://themedev.net License: GNU General Public License v2.0 License URI: http://www.gnu.org/licenses/gpl-2.0.html */
To finish, add opening and closing PHP tags to the beginning and end of your functions file. This ensures that we have laid the groundwork for when we begin writing theme functions later in this article.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.