Installing Theme Unit Test Data with wp-cli

When building custom themes client sites it is important to test as many edge case as possible. Theme Unit Test Data is the way to go here and a minimum requirement for any professionally built WordPress theme (in my opinion anyway) If building a theme for the theme repository this will almost certainly be tested. For those using WP-CLI there is a quick and easy way to install and then remove this test data. Naturally, you will need to install and setup WP-CLI before proceeding.

First you will want to backup your current database

wp db export mydatabase.sql 

Then reset your WordPress installation. This step is not obligatory as you can import the test data on top of your own data. The --y flag stands for “yes” in response to the question “Are you sure you want to reset your database?” So make sure you have the old data backed up in case.

wp db reset --y 

Next you will want to import

curl -O
wp plugin install wordpress-importer --activate
wp import ./theme-unit-test-data.xml --authors=create
rm theme-unit-test-data.xml

Here is where you will test you theme. Ideally you should go through and systematically check each of the links which have all sorts of edge cases. Correct any abnormalities and problems with layout. When you are finished you can change back to your old data.

wp db reset --y
wp db import mydatabase.sql

Please note that you will have to provide the --url flag for some of those wp commands when using multisite. I am happy to answer any questions in the comments.

3 comments on “Installing Theme Unit Test Data with wp-cli

  1. Thanks for the article!
    Are you aware if it is possible to exclude specific tables from the db export command – rather than include only the ones you want?

    Something like:

    wp db export tables=!wp_users,!wp_usermeta mydatabase.sql

    • Thanks for your comment. You might be able to play with the wildcard search as in wp db export --tables=$(wp db tables '!wp_user*' --format=csv) but it depends which wildcard characters are supported. The example here mentions * and ? Let me know if that works.

  2. Hi Kirsten, thanks for these WP-CLI snippets. They’re really useful, and I’ve used them on several sites.

Leave a Reply

Your email address will not be published.

Click to view allowed tags

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.