Creating Test Pages With WP-CLI

By: Ryan Kienstra on: June 24, 2018  in: Plugins

Coding image
When you’re developing a new feature, it sometimes helps to write a a WP-CLI script to create a test page.

This helps with:

  • Your development
  • Code reviews
  • QA

Here are some examples where creating test pages with WP-CLI can help.

Embed Test Page

On the AMP plugin, we added support for native WordPress embeds and shortcodes. So we needed a way to test the current support.

Weston Ruter created a WP-CLI script that can add them to a post.

Simply running bash bin/create-gutenberg-test-post.sh in VVV produces this test page:

Embed test page

Embed and shortcode test page

In some cases, it would have been easy to simply paste the embed URLs into the post content. But this script helps with the embeds and shortcodes with dynamic content, like the video playlist above.

Gutenberg Test Page

We needed to ensure that the AMP plugin supported all core blocks. So I created a WP-CLI script for this, based on the one above.

Luckily, Gutenberg has a great set of test fixtures, covering most blocks.

The script simply outputs those test fixtures, with slight changes:

Gutenberg test script page

Gutenberg test page

There are often permutations that you’ll need to test. For example, a “Categories” block with a dropdown (shown above). This function adds some.

Testing Widgets

Creating test widgets with a script can be really useful. We needed to test all Core widgets, and several permutations of them.

This WP-CLI script adds the widgets.

It also prevents duplication by checking if a widget with the same values already exists.

Test widgets

Test Pages With WP-CLI

These can seem like too much for simple features. But they’re really useful when:

  • There are several iterations of development (the embed support spanned several GitHub issues and developers)
  • You’re testing in several environments
  • You need to later test for regressions

This investment can save you a lot of time in testing and development.


Leave a comment