The most effective method to Start Testing Your WordPress Code With the Pest PHP Testing Framework

WordPress php

We can all concur that WordPress has made considerable progress since its start and that it developed into something much more than publishing content to a blog programming.

At its center, it’s as yet a substance the board framework (CMS), however with north of 59,000 modules in the registry, you can tweak it to be significantly more.

The justification behind its prominence is its low passage hindrance for both substance makers and designers. Here and there this accompanies an expense. Its a well known fact that WordPress has a terrible standing with regards to improvement. It has a ton of inheritance stuff and stalwart guidelines that forestall any retrogressive similarity breaking change with regards to PHP code (Gutenberg is another story I will not get into).

That inheritance PHP code is in many cases utilized by the engineers that are beginning to enter the universe of programming, and the issue with that is they can get familiar with some terrible programming designs. That thus implies they will reuse the inadequately composed code, expanding how much terrible code on the planet.

This is where WordPress gets its awful standing in the designer local area.

Breaking the cycle

So how might we break this pattern of terrible code? By showing new designers how they ought to compose great code. One instance of showing new designers (yet additionally old ones that are as yet sticking to the ‘WordPress’ approach to getting things done) is by composing instructional exercises.

Another way is to urge them to utilize apparatuses that can assist them with composing better code.

I’m presently associated with the work which intends to deliver the new adaptation of the WordPress Coding Standards, a bunch of rules utilized for the PHP_CodeSniffer apparatus that will inform you as to whether your code has a few expected issues (security, best practices, code style).

Another apparatus that I’ve as of late evolved is a bundle that will assist engineers with setting up WordPress combination tests that utilization the Pest testing structure.

Ok, so why do we need this new tool?

The primary inspiration driving making this bundle is to urge more individuals to compose tests for their code, particularly module designers.

A ton of designers in the WordPress people group go with the mantra: I can see that it works since I’ve given it a shot in my program. That is OK, however there are issues with that.

In the first place, it’s tedious. Each time you roll out some improvement, you really want to ensure it works, yet additionally that you broke nothing.

Second, individuals commit errors. We are not idiot proof, and code might be abused in manners you never imagined. You would be astounded at how innovative individuals can be while composing code.

Mechanized tests are fast and can assist you in testing different cases that will with happening when you execute your code.

You test for the planned way of behaving (cheerful way), and in a fast way, you can add instances of how your code can be utilized in a manner you didn’t expect it to be utilized (miserable way).

It additionally shields your code from relapses. A code relapse is the point at which you accidentally decipher one piece of your code by adding new code.

The problem with tests set up so far

Testing in WordPress is anything but another thing. Furthermore, dislike you were unable to set up tests for your code previously. There are astounding libraries out there that will assist you with setting everything up like wp-program.

Yet, the issue is that the arrangement strategy is frequently awkward.

You really want to set up a different data set for tests, and you want to run specific contents, then, at that point, change records to make everything work.

Can we just be real, it’s anything but something clear to do, and designers are essentially lethargic animals (that is the reason we compose code to get things done for us 😄).

The point of the wp-bug combination test arrangement is to dispense with all the excess work.

How to set it up

In order to set it up, your project must use Composer. It’s a de-facto standard way of adding packages to your code.

In your terminal type

composer require dingo-d/wp-pest-integration-test-setup --dev

After you’ve downloaded the package and its dependencies you can set up the theme tests by typing

vendor/bin/wp-pest setup theme

Or, in the case you want to set up tests for your plugin, you can write

vendor/bin/wp-pest setup plugin --plugin-slug=your-plugin-slug

Alternatively, you can give a – – wp-variant boundary, to indicate which WordPress adaptation you might want to test your code on.

Behind the scenes, a WordPress case will be downloaded, and an in-memory information base will be set up, alongside two instances of tests that you can run.

Then, running either

vendor/bin/pest --group=unit


vendor/bin/pest --group=integration will run the tests. The excellence of Pest is that its punctuation is designer amicable. It has astounding documentation and incredible punctuation. How about we check a straightforward model out. Let's assume you are enrolling a custom post type called 'Books':

 * Plugin Name: Test plugin
 * Desctiption: Test plugin
 * Version: 1.0.0
 * License: MIT

function test_plugin_register_books_cpt() {
    $args = array(
        'label'              => esc_html__( 'Books', 'test-plugin' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    register_post_type( 'book', $args );
add_action( 'init', 'test_plugin_register_books_cpt' );

After running the setup command that adds an example, a test called BooksCptTest.php would look like this:


namespace Tests\Integration;

beforeEach(function () {

afterEach(function () {

test('Books custom post type is registered', function () {
	// We can use assertions from PHP_Unit.
	$this->assertNotFalse(has_action('init', 'test_plugin_register_books_cpt'));

	$registeredPostTypes = \get_post_types();

	// Or we can use expectations API from Pest.

Running vendor/bin/pest --group=integration gives us the following output:

Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.

   PASS  Tests\\Integration\\BooksCptTest
  ✓ Books custom post type is registered

  Tests:  1 passed
  Time:   0.14s


What’s more, very much like that, you can run WordPress combination tests in your topic or module. Tests are astonishing on the grounds that besides the fact that they defending are us from botches, yet they likewise compel us to compose perfect and testable code. This is particularly valid for modules that have confounded rationale or are speaking with outsider APIs.

Composing tests for such a codebase will drive you to contemplate what your code engineering resembles so you can undoubtedly compose mechanized tests – also the time and cash you’ll save from not having to test everything physically.

On the off chance that you think this is the sort of thing you could profit from, go ahead and use it, and star the archive on GitHub.

Ideally, this will urge more WordPress engineers to utilize devices that will upgrade their coding abilities.


Leave a Comment