4 Ways To Generate Test Data in WordPress

Posted on:

When you're building a WordPress theme or a WordPress plugin, there comes a time where you need dummy data added to the site for testing purposes. If you're spending your time manually adding this data to the database, you're spending a lot of time setting up your site.

Generating test data in WordPress can be very easy if you have the right tools for the job, and in many cases you don't have to write a single line of code to generate the data. Let's get into the ways test data can be generated on your WordPress website.

WordPress Theme Unit Test Data

The first method everyone should know about is the theme unit test data file for WordPress. This file is an XML file that can be used to import a series of blog post content into your site, and the content it generates is specifically designed to catch edge cases that are otherwise easy to miss.

Not only is this method fast, but it's also highly effective. This is literally the dataset that is used by the theme review team to confirm a theme can be added to the WordPress theme repository, and it's very thorough. It details different features of WordPress, including lesser-known things, and serves as an excellent checklist of things to ensure your theme supports before you publish it live.

I use this on every theme I build, even if the site has existing post content. This allows me to test directly against edge cases, and identify problems with my theme.

Plugin-Specific Test Data

The theme unit test data file is an awesome starting point, but it only works with core files. In-general, it's best to try to use pre-made plugin data instead of auto-generated data. This is because when test data is made manually, it tends to try to catch edge cases, randomly-generated data is just that - random.

So, when I'm working with a plugin, I will first check to see if the plugin includes sample data. It's a long shot, and most do not do this, but I take a quick moment to check anyway. WooCommerce, for example, has sample data that creates a variety of products in your site, and is particularly effective for testing your WooCommerce theme.

WP-CLI

If you can't find manually-generated data, the next best thing is to generate data automatically using WP-CLI. You can use wp post generate to generate post data automatically. This includes any of your custom post types, and you can provide pretty much any random set of arguments to make it generate data in a particular fashion.

You can also generate other structures, like comments with wp comment generate, or taxonomy terms terms using wp term generate. You can even generate a bunch of fake users with wp user generate.

This works pretty darn well in some cases. If you just need to generate data one time, and you're comfortable with bash, you can use these commands along with other commands like wp post term add to add terms to generated posts.

Roll Your Own With WP-CLI and Faker

If you're building something custom, or the CLI generator is just not random-enough, your next best option is to make your own generator using PHP and the Faker library. This allows you to write a PHP script that you can re-use in the future. Odds are, if you need to generate this data now, you'll probably need to generate it again, so why not take a little extra time to put together a script to automate the process?

In my beer plugin course one of the first things we do with our custom post type is create a WP CLI generator using this exact method. It's an instrumental step in testing our beer custom post type, and saves us a lot of time as we go through and build the plugin.

I like to create WP CLI commands for my custom generators. It allows me to make my generators as powerful as I want without needing to spend a bunch of time creating an interface. Which is nice, since the entire reason why we're making a generator is to save time, right?

Conclusion

There are many different ways to fill your WordPress database with test data. Using a combination of these strategies can not only save you time, but it can also help you build more-stable websites because you have better testing tools at your disposal. Take the time to build generators you need, and if you set them up correctly, you'll probably re-use them for other projects in the future.